Reputation:
I want to download an html file from another website , I have tried the following code but it's not working , It just redirects to the website:
<a href='http://example.com' download>Download</a>
So how to download an html file from another website?
Upvotes: 1
Views: 6034
Reputation: 383
As explained in the comments, the download attribute only works for same origin URLs.
I solved this by exposing an endpoint in the backend that redirects to the url with the file I want to download, and pointing to that on the client side.
Frontend
<a href='/download-file' download>Download</a>
Backend
router.get('/download-file', (req, res, next) => {
res.redirect('http://example.com/url-to-the-file');
});
Beware of what you are redirecting to if you don't have control over those files.
In my case the file was hosted in a CDN, which made the origin of the URL different, but I still had control over it.
Upvotes: 4
Reputation: 663
The <a>
is for directing a user to a specified link. In html5 you may add the property download
and link to a specific file to signify that you are intending to serve the file as a download.
In this case the href value should point to a specific file and not just a directory. The html file with the link must also be marked with a doctype for html5 at the top <!DOCTYPE html>
.
You must serve the html/download from the same domain as your site or else deal with CORS which is another topic.
Example
<a href='http://example.com/myFileToDownload.html' download>Download Source</a>
Upvotes: -2