• post请求(下载文件)


    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>
  • 相关阅读:
    mysql分表技术
    TP5.0 excel 导入导出
    整理:手机端弹出提示框,使用的bootstrap中的模态框(modal,弹出层),比kendo弹出效果好
    Bootstrap表单验证插件bootstrapValidator使用方法整理
    input属性为number时,如何去掉+、-号?
    input 属性为 number,maxlength不起作用如何解决?
    mysql给root开启远程访问权限
    thinkphp——通过在线编辑器添加的内容在模板里正确显示(只显示内容,而不是html代码)
    解决网站请求速度慢的一些方法
    JS封闭函数、闭包、内置对象
  • 原文地址:https://www.cnblogs.com/a1-top/p/14281003.html
Copyright © 2020-2023  润新知