Home » Javascript » how to download a file from url using javascript or jquery?

how to download a file from url using javascript or jquery?

Posted by: admin August 19, 2018 Leave a comment

Questions:

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",
    contentDisposition: 'attachment; filename=' + 
        url.split("/").pop()
})
.done(function(){console.log('successfully downladed')})
.fail(function(){ console.log(`request failed`)});

Even I tried with javascript it’s not working

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