How to get File using Blob Angular

Viewed 30

I have a form and download file button my problem is why i cant sent what i input in my form at the same time with my download file?

  this.fg = this.fb.group({
        rptReqCode:[''],
        rptCode:[''],
        parFldVal:[''],
        genType:[]
    })
  }



downloadFile( filename: string = null): void{

  const token = 'my JWT';
  const headers = new HttpHeaders().set('authorization','Bearer '+token);
  this.http.get(this.appsetting.baseURL + 'File/Download'  ,{headers, responseType: 'blob' as 'json'}).subscribe(
      (response: any) =>{
          let dataType = response.type;
          let binaryData = [];
          binaryData.push(response);
          let downloadLink = document.createElement('a');
          downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, {type: dataType}));
          if (filename)
              downloadLink.setAttribute('download', filename);
          document.body.appendChild(downloadLink);
          downloadLink.click();
      }
)}
1 Answers

Usually if you want to send data to the server you use POST, if you want something for the server you use POST.

You have a couple of options:

POST user data, then use switchMap to concatenate the request and the download you file using the GET request.

Another option is to use the POST request to send your data and as a response you will receive the file. You have to make some changes in your Backend.

Related