1

Here is my service method to download the excel file but it does not work. i can see my excel file in response body but i can not download it as a file, use blob not work and only download a some wrong characters. Any help or pointer would be much appreciated.

   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);
              });
      });
  }
Sean Ch
  • 593
  • 9
  • 21
  • http://stackoverflow.com/questions/35138424/how-do-i-download-a-file-with-angular2 – Sean Ch Feb 11 '17 at 07:17
  • and this http://stackoverflow.com/questions/34149741/is-it-possible-to-receive-blob-responses-using-angular-2s-http – Sean Ch Feb 11 '17 at 07:18

0 Answers0