rndme / download

file downloading using client-side javascript
MIT License
2.27k stars 416 forks source link

Incorrect mime type HTML5 anchor #89

Open acourdavault opened 5 years ago

acourdavault commented 5 years ago

Hello,

When using this library to download a FileResponse from Django returning a PDF file content I realized that the MIMI type was ignored.

download(myBlob, "labels.pdf", 'application/pdf');

I realized that in the paragraph L101-116 the attribute 'type' was not set. 1 line change fixes this


        function saver(url, winMode){

            if ('download' in anchor) { //html5 A[download]
                anchor.href = url;
                anchor.setAttribute("download", fileName);
//MODIFICATION START
                anchor.setAttribute("type", mimeType);
//MODIFICATION END
                anchor.className = "download-js-link";
                anchor.innerHTML = "downloading...";
                anchor.style.display = "none";
                document.body.appendChild(anchor);
                setTimeout(function() {
                    anchor.click();
                    document.body.removeChild(anchor);
                    if(winMode===true){setTimeout(function(){ self.URL.revokeObjectURL(anchor.href);}, 250 );}
                }, 66);
                return true;
            }
erilot commented 4 years ago

This is a year old now, but can confirm the same (I'm downloading file responses from Guzzle, but same idea). The above proposed solution fixes it and the downloads work flawlessly when I set the mimetype to "application/octet-stream". Without adding this line, the download is just a textfile containing the filepath.

Possibly also related to https://github.com/rndme/download/issues/86 and https://github.com/rndme/download/pull/78.