将响应内容下载为角度2的文件?

时间:2017-02-11 06:19:41

标签: angular

这是我下载excel文件的服务方法,但它不起作用。我可以在响应正文中看到我的excel文件,但我无法将其作为文件下载, 使用blob不起作用,只下载一些错误的字符。任何帮助或指针都将非常感激。

   filtersUserThreadToBeExport(filterString: string, sortField: string, sortOrder: number, answered ? : Boolean): Promise < any > {
      let headers: Headers = new Headers();
      headers.append('Content-Type', 'application/json');
      headers.append('Accept', 'application/excel; charset=UTF-8');
      let searchParams: URLSearchParams = new URLSearchParams();


      let hints: string = "hint=FETCH_EAGERLY:createUser;hint=FETCH_EAGERLY:anonymousInfo;hint=FETCH_EAGERLY:anonymousInfo.city;hint=FETCH_EAGERLY:anonymousInfo.province;hint=FETCH_EAGERLY:createUser.city;hint=FETCH_EAGERLY:createUser.province;hint=FETCH_EAGERLY:createUser.contacts;hint=ORDER_DESC_BY:createDate";
      if (sortField) {
          if (sortOrder > 0) {
              // ascending
              hints = "hint=FETCH_EAGERLY:createUser;hint=FETCH_EAGERLY:anonymousInfo;hint=FETCH_EAGERLY:anonymousInfo.city;hint=FETCH_EAGERLY:anonymousInfo.province;hint=FETCH_EAGERLY:createUser.city;hint=FETCH_EAGERLY:createUser.province;hint=FETCH_EAGERLY:createUser.contacts;hint=ORDER_ASC_BY:" + sortField;
          } else {
              // descending
              hints = "hint=FETCH_EAGERLY:createUser;hint=FETCH_EAGERLY:anonymousInfo;hint=FETCH_EAGERLY:anonymousInfo.city;hint=FETCH_EAGERLY:anonymousInfo.province;hint=FETCH_EAGERLY:createUser.city;hint=FETCH_EAGERLY:createUser.province;hint=FETCH_EAGERLY:createUser.contacts;hint=ORDER_DESC_BY:" + sortField;
          }
      }

      let filter: string = ";filter=";

      filter = filter + "deleted:" + false;

      if (filterString) {
          if (filterString !== null) {
              filter = filter + "&" + filterString;
          }
      }
      let queryParamString: string = "";
      if (answered != null) {
          if (answered) {
              queryParamString = "answered=true";
          } else {
              queryParamString = "answered=false";
          }
      }
      headers.append('X-HTTP-QueryString-Override', encodeURI(`${filter};${hints}?${queryParamString}`));
      let targetUrl: string = `${this.endpoint}/user-threads/filtered-threads`;
      let requestOptions = new RequestOptions({
          method: RequestMethod.Get,
          url: targetUrl,
          headers: headers,
          search: searchParams
      });
      return new Promise((resolve, reject) => {
          this.http.request(new Request(requestOptions))
              .map(res => new Blob([res], {
                  type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
              }))
              .subscribe(blob => {
                  resolve(blob);
              }, error => {
                  reject(error);
              });
      });
  }

0 个答案:

没有答案