Skip to content
Advertisement

Download File automatically with browser that can be opened or read properly

Frontend will send a post request to Scala Play Framework API to download a file. The response header is like :

JavaScript

My react js code is as below:

JavaScript

It can be downloaded automatically but the file cannot be read. For example, if I download an image, the image cannot be rendered. If I download a zip file, it cannot be extracted. I already tried React-FileDownload, FileSaver, convert the res.data into arraybuffer with the creation of 8 bit array for loop – I.Just.Cant.Make.It.Work.

When I erase the extension format from ubuntu and open it on Atom, these shows up. And from the download tab in Chrome, it stated blob:http://localhost:8000/4de5d808-67a6-4d4e-9920-24bd342664f6

JavaScript

Advertisement

Answer

I solved the problem. It lies in the POST request.

JavaScript

Have to add responseType: 'blob'

I changed to FileSaver to download file.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement