How to convert buffer to file when downloading on React side
P粉006847750
2023-09-01 23:23:15
<p>I'm using<a href="https://www.npmjs.com/package/convert-html-to-pdf">https://www.npmjs.com/package/convert-html -to-pdf</a> to convert html to pdf in nodejs. I have a react frontend and nodejs backend. I want to convert the buffer into a file to be downloaded by the user on the react side. What should I do? I don't want to save the file on my server. </p>
We can set the header
Content-disposition
attachment
to indicate that the response is a downloadable file.Backend: Express Example
Front end: React example
If the endpoint is a POST method,
window.open
will not work. We have to use a form: