environment.ts:
export const environment = { production: false, envName: 'dev', baseURL: 'http://xxxxxxxxxxxxxx/', baseURL1: 'http://xxxxxxxxxxxxxxxxxxx/', loginURL: 'http://xxxxxxxxxxxxxxxxxxxx/' };
API.ts:
export: environment.baseURL1+'manage/export',
data.service.ts:
getExportpage(params): Observable<any> {
return this.http.Post(API.exportpage, params, 0, { msg: '報表導出列表', data: {} }).pipe(map(res => res.data)); }
http.service.ts:
/** * 請求主體可以為 urlencoding、JSON、FormData 的 POST 請求 * @param _url|string 請求地址 * @param _params|ParamsType 參數對象 * @param contentType|ContentType Post請求body編碼格式 * @param safeResponse|HttpResponseType 請求錯誤時的返回值 * @param userNotification|boolean 是否顯示操作提示彈框 * @param errorSource|string 引起錯誤的操作來源 */ Post(_url: string, _params: ParamsType, contentType: PostContentType, safeResponse: HttpResponseType, userNotification: boolean = false, errorSource: string = ''): Observable<HttpResponseType> { const URL = this.URLPattern.test(_url) ? _url : environment.baseURL + _url; const contentTypeArray = [new HttpParams({fromObject: _params}), _params, this.toFormData(_params)]; let messageID = ''; // if (userNotification) { // messageID = this.message.loading('添加中...', { nzDuration: 0 }).messageId; // } return this.httpClient.post(URL, contentTypeArray[contentType], { }).pipe( map((response: HttpResponseType) => { return this.responseHandler(response, messageID); }), catchError(this.handleError(safeResponse, errorSource, messageID)) ); } /** * Post 請求參數處理成 FormData 函數 * @param _params|any */ private toFormData(_params: any): FormData { if (!_params) { return new FormData(); } if (_params.constructor === FormData) { return _params; } const formData = new FormData(); for (const key in _params) { if (_params.hasOwnProperty(key)) { formData.append(key, _params[key]); } } return formData; } /** * 請求成功情況處理 * @param response|HttpResponseType * @param messageID|string */ private responseHandler(response: HttpResponseType, messageID: string): HttpResponseType { // this.message.remove(messageID); // this.message.success(response.msg); return response; }
xx.component.ts:
/** * Post下載 */ dwReport() { this.dataService.getExport({ end: this.end, granularity: this.type, platform: this.platform, start: this.start }).subscribe(data => { let url = window.URL.createObjectURL(data); let link = document.createElement('a'); link.style.display = 'none'; link.href = url; link.download = this.platform + '報表.xls'; // docNumber 動態文件名 document.body.appendChild(link); link.click(); }); }
xx.component.html:
<div class="tit_download"> <a (click)="dwReport()"> 點擊下載 </a> </div>