4

I'm creating a webpage that has to download a file in CSV format. The REST API returns the file.

I get an error while accessing the API. I guess that is because the file is getting converted to JSON. How can I get it from the back end and handle it?

service.ts

return this.http.get(URL);        
2
  • 1
    What you have done so far? Commented May 27, 2019 at 6:40
  • 1
    Possible duplicate of Angular 6 CSV download Commented May 27, 2019 at 6:40

2 Answers 2

8

You can use file-saver

import * as FileSaver from 'file-saver';

this.http.post(url, resource, { responseType: 'blob' }) .subscribe((resp: any) => { FileSaver.saveAs(resp, `filename.csv`) }); 
Sign up to request clarification or add additional context in comments.

1 Comment

how can i get file name from response headers?
0

To convert octet-stream to any required data type.
I had a similar situation where i converted OCTET-STREAM into pdf.

Slight difference, might be helpful for others, I received json response and octet-stream Array was a field in that json response.

In this case, replace .pdf by .csv

octet_array is the array of octet values.

 let arr = new Uint8Array(octet_array); let downloadLink = document.createElement('a'); let b = new Blob([arr], { type: 'application/octet-stream' }) downloadLink.href = window.URL.createObjectURL(b); downloadLink.setAttribute('download', "prescription.pdf"); document.body.appendChild(downloadLink); downloadLink.click(); downloadLink.parentNode.removeChild(downloadLink); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.