Skip to content Skip to sidebar Skip to footer

How To Download A File From Url Using Javascript Or Jquery?

I used jquery fileDownload plugin to download a file from URL. But it's not working. It is always failing please help with this $.fileDownload(url,{ contentType: 'text/csv',

Solution 1:

Your JavaScript does not work probably because you append a to body before you add href and download attributes.

Append just before triggering click

Also remember that this will only work on files with the same-origin URLs (Source).

This attribute only works for same-origin URLs.

var a = document.createElement("a");
a.href = url;
fileName = url.split("/").pop();
a.download = fileName;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
a.remove();

Post a Comment for "How To Download A File From Url Using Javascript Or Jquery?"