最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to download a file on Chrome without auto renaming file to "download"? - Stack Overflow

programmeradmin3浏览0评论

I use javascript to generate a file and download it. It seems, that depending on the version of chrome, the download file names can be auto renamed to 'download'. Is there a way to avoid it?

this is my code:

var link = document.createElement("a");
link.setAttribute("href", 'data:application/octet-stream,' + 'file content here');
link.setAttribute("download", 'file1.txt');

link.click();

This is not a duplicated question, because I'm using the latest Chrome and the previously suggested hyperlink is exactly what I'm using. I think, Chrome v34 works fine, but once my Chrome auto updated to v35, it went back to 'download' file name.

I use javascript to generate a file and download it. It seems, that depending on the version of chrome, the download file names can be auto renamed to 'download'. Is there a way to avoid it?

this is my code:

var link = document.createElement("a");
link.setAttribute("href", 'data:application/octet-stream,' + 'file content here');
link.setAttribute("download", 'file1.txt');

link.click();

This is not a duplicated question, because I'm using the latest Chrome and the previously suggested hyperlink is exactly what I'm using. I think, Chrome v34 works fine, but once my Chrome auto updated to v35, it went back to 'download' file name.

Share Improve this question edited May 30, 2014 at 8:57 Alex Kulinkovich 4,78815 gold badges49 silver badges54 bronze badges asked May 29, 2014 at 17:35 user3688566user3688566 1011 silver badge4 bronze badges 3
  • What version of Chrome are you using? The download attribute should have support going back to v31: caniuse./#feat=download – anotherdave Commented May 29, 2014 at 17:46
  • If you were already using one of the solutions suggested in the other question, and it is just not working anymore with a new Chrome version, then you should have mentioned that upfront. – C3roe Commented May 29, 2014 at 21:59
  • Seems like a bug with the download attribute, you should report a bug at code.google./p/chromium/issues/list – Fabrício Matté Commented May 30, 2014 at 1:57
Add a ment  | 

2 Answers 2

Reset to default 2

It seems to be linked to this bug/feature. Status: Wontfix.

Use HTML5 download attribute. This attribute will tell browser that virtual link we created is aimed for download only. It will download file from link's href to file with name specified as download attribute's value. This great feature works in Chrome.

window.downloadFile = function(sUrl) {

    //If in Chrome or Safari - download via virtual link click
    if (window.downloadFile.isChrome || window.downloadFile.isSafari) {
        //Creating new link node.
        var link = document.createElement('a');
        link.href = sUrl;

        if (link.download !== undefined){
            //Set HTML5 download attribute. This will prevent file from opening if supported.
            var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
            link.download = fileName;
        }

        //Dispatching click event.
        if (document.createEvent) {
            var e = document.createEvent('MouseEvents');
            e.initEvent('click' ,true ,true);
            link.dispatchEvent(e);
            return true;
        }
    }

    // Force file download (whether supported by server).
    var query = '?download';

    window.open(sUrl + query);
}

window.downloadFile.isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
window.downloadFile.isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;

Demo Link: http://pixelsmander./polygon/downloadjs/#.U4gyDPmSwgS

发布评论

评论列表(0)

  1. 暂无评论