Javascript 如何在Chrome上下载文件而不将文件自动重命名为;下载;?
我使用javascript生成一个文件并下载它。 看起来,根据chrome的版本,下载文件名可以自动重命名为“下载”。有办法避免吗 这是我的代码:Javascript 如何在Chrome上下载文件而不将文件自动重命名为;下载;?,javascript,jquery,html,google-chrome,Javascript,Jquery,Html,Google Chrome,我使用javascript生成一个文件并下载它。 看起来,根据chrome的版本,下载文件名可以自动重命名为“下载”。有办法避免吗 这是我的代码: var link = document.createElement("a"); link.setAttribute("href", 'data:application/octet-stream,' + 'file content here'); link.setAttribute("download", 'file1.txt'); link.cli
var link = document.createElement("a");
link.setAttribute("href", 'data:application/octet-stream,' + 'file content here');
link.setAttribute("download", 'file1.txt');
link.click();
这不是一个重复的问题,因为我使用的是最新的Chrome,而之前建议的超链接正是我使用的。我认为ChromeV34可以正常工作,但一旦我的Chrome自动更新到v35,它就会返回到“下载”文件名。使用HTML5下载属性。此属性将告诉浏览器,我们创建的虚拟链接仅用于下载。它将从链接的href下载文件到名为下载属性值的文件。这项伟大的功能在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;
演示链接:它似乎链接到。状态:Wontfix。您使用的是什么版本的Chrome?下载属性应该支持返回到v31:如果您已经在使用另一个问题中建议的解决方案之一,并且它不再适用于新的Chrome版本,那么您应该提前提到这一点。似乎下载属性是一个bug,您应该在