eligrey / FileSaver.js

An HTML5 saveAs() FileSaver implementation
https://eligrey.com/blog/saving-generated-files-on-the-client-side/
Other
21.65k stars 4.38k forks source link

file-saver saveAs : Error while opening the pdf #662

Open AnkitBagaria24 opened 4 years ago

AnkitBagaria24 commented 4 years ago

I am saving a pdf to my local machine, using saveAs from 'file-saver' library. Data is provided locally, it is not fetched from server.

Pdf is getting saved successfully, but while opening the pdf I am getting an error as File type plain text document (text/plain) is not supported though I am mentioning type as pdf.

Below is the code. I am developing the application using reactjs.

import { saveAs } from 'file-saver';

 const blob = new Blob([ <Invoice data={data} customer={customer}
 shipped={shipped} business={business} /> ], { type: 'application/pdf'
 })

   saveAs(blob, "trial.pdf" );

Attatching screenshot of the error

error

newuser54 commented 3 years ago

Same problem bro, did you solve it?

egontrimfa-reea commented 3 years ago

Any solution yet?

newuser54 commented 3 years ago

I solved using fileDownload ( https://www.npmjs.com/package/js-file-download ) and setting the axios response as "blob", here my code:

  const res = await axios.get(
          `/your/pdf/route`,
          {
            responseType: "blob"
          }
        );
  await fileDownload(res.data, `${pdfName}.pdf`);

Works fine, i think file-saver works too, the problem maybe was the blob type.

qzw881130 commented 1 year ago

I solved it.

import { saveAs } from 'file-saver';

axios.post('/get-file-content', formdata, {responseType: "blob"}).then((resp) => {
        console.log('bokunReq', resp.data)
        saveAs(resp.data, "ticket.pdf");
    })

this is core {responseType: "blob"}