3

I used jQuery fileDownload plugin to download a file from URL.

$.fileDownload(url,{
    contentType: "text/csv",
    contentDisposition: 'attachment; filename=' + 
        url.split("/").pop()
})
.done(function(){console.log('successfully downladed')})
.fail(function(){ console.log(`request failed`)});

I even tried with JavaScript but 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();
1
  • a.setAttribute("download", "filename"); Commented Oct 19, 2019 at 14:28

2 Answers 2

3

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();
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this one it's working for the image. But If I want to download a file from the external URL. It is not working. It's just opening that file into the new tab. I want to download instead of opening in new tab.
On the page I gave you the link to is everything explained. Did you read it? You can't download files from external URL using <a download...></a>
yeah!! is there any other way to download the file even without using <a download... />
Try this
This is very helpful and appreciated. Worth noting: apparently if you do this with multiple files programmatically, no matter how much delay you put in between subsequent calls, only the last file actually gets downloaded. (As tested in Chrome 73 on Mac).
0

This code snippet will download from external source

<a onclick="saveFile('url')">Download</a>

<script>
    function saveFile(url) {
    // Get file name from url.
    var filename = url.substring(url.lastIndexOf("/") + 1).split("?")[0];
    var xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = function() {
        var a = document.createElement('a');
        a.href = window.URL.createObjectURL(xhr.response); // xhr.response is a blob
        a.download = filename; // Set the file name.
        a.style.display = 'none';
        document.body.appendChild(a);
        a.click();
        delete a;
    };
    xhr.open('GET', url);
    xhr.send();
    }
</script>

the saveFile('url') takes string url as an argument so pass in the correct url and your file will download directly. Worked for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.