How to create a file downloader via URL using pure javascript

Ashutosh Tiwari
2 min readMay 26, 2022

--

Hello friends, today in this blog you will learn how to create a file downloader via URL using pure Javascript. In our previous blog, we saw how to create drag & drop or sortable lists using HTML, CSS, and Sortable JS. Earlier, I shared many projects related to javascript, you can check if you want, and don’t forget to check HTML, CSS, and Javascript projects.

This tool (File Downloader via URL) can download any file like image, video, pdf, SVG, etc. Users have to paste a valid URL of the file and click the download button to download the file. Remember, the file should be publicly accessible to download.

At first, I got the user entered file URL, and using fetch() API, I fetched the file. Once I got a response, I return the response as a blob() and in another then method, I got an object that contains details of the file.

You may like these:

Then, using URL.createObjectURL() method, I created a URL of that file object. This URL is stored in the document window. At last, I created a <a> tag and stored the URL as the href value of this tag, and click it so the file download. Watch the above video for a detailed explanation of each JavaScript line.

Note: If you get a cors (cross-origin resource sharing) error in the console during file downloading, that means the browser blocked the request because the requested site doesn’t allow you to access that file.

This file downloader is made with pure JavaScript no server-side language is used to create it. If you liked it and want to create So you can check the source code or preview.

The Preview is available here.

--

--

No responses yet