1

I am trying to download a file using JS, without linking to it in HTML and without creating temporary DOM elements like some of the old tricks suggest.

I found this snippet that almost works, it lets me download the file but it assigns it a generated name. Adding the Content-Disposition option to header works for the fetch part but it is disregarded later with blob.

const options = {
    headers: {
        "Content-Disposition": 'attachment; filename="report.pdf"'
    }
}
function downloadPdf() {
    fetch("./report.pdf", options)
        .then(res => res.blob())
        .then(blob => {
            let file = window.URL.createObjectURL(blob);
            window.location.assign(file);
        });
}
downloadButton.addEventListener("click", downloadPdf, false);

How can I pass the name of the file from Content-Disposition header to the created blob?

miran80
  • 945
  • 7
  • 22
  • https://stackoverflow.com/a/52273870/120242 it's a known issue and there doesn't seem to be a known workaround – user120242 May 30 '20 at 01:20
  • Does this answer your question? [JavaScript blob filename without link](https://stackoverflow.com/questions/19327749/javascript-blob-filename-without-link) – user120242 May 30 '20 at 01:20

0 Answers0