1

This javascript code does not download the actual file, but instead it puts the path+filename of the URL into the contents of a file named requestRouter_amd64.msi:

 <p>
 <script type = "text/javascript">

    async function downloadFile(filePathAndName, fileDataContentType, fileName) {
    try {
        fileDataContentType = "octet/stream"
            const blob = new Blob([filePathAndName], {
            type: fileDataContentType
        });
        const link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = fileName;
        link.click();

    } catch (error) {}
}


downloadFile("https://download.microsoft.com/download/E/9/8/E9849D6A-020E-47E4-9FD0-A023E99B54EB/requestRouter_amd64.msi", "application/zip", "requestRouter_amd64.msi");


 </script>
</p>

I also tried "application/zip" for the fileDataContentType but same result.

JeffR
  • 765
  • 2
  • 8
  • 23
  • 1
    That's what the `Blob` constructor does. It doesn't actually do any network interaction. You need to use `fetch` or `XMLHttpRequest` to download the contents of a file to bytes, then pass the array of bytes to the `Blob` constructor. – Heretic Monkey Dec 15 '20 at 23:22
  • 1
    Does this answer your question? [Download Binary Files with Javascript](https://stackoverflow.com/questions/17696516/download-binary-files-with-javascript) – Heretic Monkey Dec 15 '20 at 23:30
  • I wish, I just don't know what, in that link, in option 1 and 2 on the linese open("GET", they have requestUrl. Is requestUrl supposed to be filled in withthe full url to the file? Like http://download.domain.com/file.msi? And if so, is it surrounded by single or double quotes? – JeffR Dec 15 '20 at 23:35
  • Just pass `filePathAndName` for requestUrl. – Heretic Monkey Dec 15 '20 at 23:39
  • You mean I put the xhr code into my function downloadFile? – JeffR Dec 15 '20 at 23:40
  • Sure. Just put your code with the blob into the `xhr.onload` event handler function, inside the `if (this.status === 200) {` block. – Heretic Monkey Dec 15 '20 at 23:43
  • How would I put it all together? I'm getting confused. :( – JeffR Dec 15 '20 at 23:52
  • Since you want to download from a link, simply set `link.href = filePathAndName;`. No blobs, xhr, streams or anything. – Bergi Dec 16 '20 at 13:14

1 Answers1

0

The blob, from what I've seen, consumes client memory. So I followed the last entered solution at this solution to download the file via a stream. Works like a treat, even though I did not use the CSS to move the link.

JeffR
  • 765
  • 2
  • 8
  • 23