如何在Chrome上下载文件而不自动重命名文件以“下载”?

我使用javascript生成文件并下载它。 看来,根据chrome的版本,下载文件名可以自动重命名为“download”。 有没有办法避免它?

这是我的代码:

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

这不是一个重复的问题,因为我使用的是最新的Chrome,之前建议的超链接正是我正在使用的。 我认为,Chrome v34运行正常,但是一旦我的Chrome自动更新到v35,它就会回到“下载”文件名。

使用HTML5下载属性。 此属性将告诉浏览器我们创建的虚拟链接仅供下载。 它将文件从链接的href下载到文件,其名称指定为下载属性的值。 这个伟大的function适用于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; 

演示链接: http : //pixelscommander.com/polygon/downloadjs/#.U4gyDPmSwgS

它似乎与此错误/function相关联。 状态:Wontfix。