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.

8
  • 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. Commented Dec 15, 2020 at 23:22
  • 1
    Does this answer your question? Download Binary Files with Javascript Commented Dec 15, 2020 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 download.domain.com/file.msi? And if so, is it surrounded by single or double quotes? Commented Dec 15, 2020 at 23:35
  • Just pass filePathAndName for requestUrl. Commented Dec 15, 2020 at 23:39
  • You mean I put the xhr code into my function downloadFile? Commented Dec 15, 2020 at 23:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.