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>