I have a base64 string, file type. File type can be image, text or even pdf. I need to show download link and when user clicks it should start downloading as expected file. Concisely, server sends me file as base64 string, and I need to save it as file on browser. How can I save base64 string as file on browser? It would be best if solution works on IE9 also.
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
Adapted from https://gist.github.com/RichardBray/23decdec877c0e54e6ac2bfa4b0c512f to work on Firefox.
Đang xem: Express js upload and save base64 image into file system
function downloadBase64File(contentBase64, fileName) { const linkSource = `data:application/pdf;base64,${contentBase64}`; const downloadLink = document.createElement(“a”); document.body.appendChild(downloadLink); downloadLink.href = linkSource; downloadLink.target = “_self”; downloadLink.download = fileName; downloadLink.click(); }
You can do this from js to download pdf.
Use:
document.location = “data:application/pdf;base64,” + base64String
You get the effect you desire (web page showing a link, and when user clicks, the save as dialog pops up) when the appropriate response headers are present when the browser requests the resource:
Content-Disposition: attachment; filename=”yourfilename.extension”
If you”re getting the file from the server as a base64 string embedded in your html, perhaps you can skip the embedding and simply embed a direct link to the file on your server, having the server serve it up to the user.
Xem thêm: Bài 6 Sbt Gdcd Lớp 8: Em Hiểu Thế Nào Là Tình Bạn Là Gì Giáo Dục Công Dân
Related SO on Content-Disposition
Thanks for contributing an answer to Stack Overflow!
Please be sure to answer the question. Provide details and share your research!
But avoid …
Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.
Xem thêm: Khung Cao Thủ Liên Quân – Bộ Banner Liên Quân Với Khung Rank Đẹp
To learn more, see our tips on writing great answers.
Post Your Answer Discard
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Site design / logo © 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev2022.10.14.26767
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.