



Same-origin policy - Web security | MDN (mozilla.I recently worked on a side project, which generates reports per user’s request.: The Anchor element - HTML: HyperText Markup Language | MDN ().URL.createObjectURL() - Web APIs | MDN ().See the Pen < a href="">Download< /a> byīy requesting to download a file and converting it into a blob form, the cross-domain restriction of the browser can be avoided, but when downloading a large file, it may take a long time, and the user needs to be actively prompted to avoid being considered a bug. Using the function to download: downloadFile('('), Create an element to download the string.Convert the blob object to a string by using URL.createObjectURL().Use fetch API to download the script file.So the steps for downloading the file will be: If we use fetch to request the data, the response can be converted to blob type. Using Blob to download fileīlob stands for Binary Large Object and is a data type that can store binary data. If the file is not of the same origin, the browser will open it rather than downloading it.įortunately, the download attribute supports the blob: scheme. download the google analytics javascript file with aīut it does not work, because download only works for same-origin URLs. It’s easy to download a file with tag, just remember to add a download attribute to tell the browser to download rather than opening the hyperlink. Using tag with download attribute (Not working) In order to demonstrate the download function, this article takes downloading the Google Analytics JavaScript file as an example. But the simpler you think, the more problems you will encounter in the implementation process. I remember downloading a file was very simple, basically the same as opening a link in a new tab. Recently I developed a feature that needs to add a download button to the page to support downloading a PDF file hosted on a CDN.
