kennethjiang / js-file-download

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

js-file-download does not downloads png/jpg files.

afrazahmad21 opened this issue · comments

Hi,
I am currently working in a react project. I want to download file via url (http://developers.iqvis.com:8082/5b339dae14dd2757b711e56f/1530185975056-iqvis.png) but downloading png file and opening it gives error (Fatal error reading a png file: Not a png file)

here is sample code

  axios.get(apiUrl)
      .then((response) => {
        fileDownlaod(response.data, fileName);
      })
      .catch((err) => {
        console.log(err);
      });

Try adding the following option to your get request :

{ responseType: 'arraybuffer' }

eg:

axios.get(apiUrl, { responseType: 'arraybuffer' } )
 .then((response) => {
   fileDownload(response.data, fileName);
 })
 .catch((err) => {
   console.log(err);
 });

That worked for me.

Should this issue be closed?

Thank you @neomorphic for offering the solution. Closing this issue now. @afrazahmad21 feel free to re-open it if the solution doesn't work for you!