Javascript html5-下载属性集文件名未定义
这可能是一个自以为是的问题。下面的示例代码运行良好Javascript html5-下载属性集文件名未定义,javascript,html,Javascript,Html,这可能是一个自以为是的问题。下面的示例代码运行良好 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <tit
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<title>title</title>
</head>
<body>
<a id="downLink"></a>
</body>
<script type="text/javascript">
function downloadFile(filename){
var anchor = document.getElementById('downLink');
anchor.setAttribute('href', filename);
//anchor.setAttribute('download','');
var icon = document.createElement('i');
icon.className = 'fa fa-cloud';
var icon1 = document.createElement('i');
icon1.className = 'fa fa-car';
anchor.append(icon);
anchor.append(icon1);
icon1.addEventListener('click', function(e){
e.preventDefault();
console.log('car clicked');
});
}
downloadFile('SampleData_12_07_2017_00_08_00.xlsx');
</script>
</html>
标题
函数下载文件(文件名){
var anchor=document.getElementById('downLink');
setAttribute('href',文件名);
//setAttribute('download','');
var icon=document.createElement('i');
icon.className='fa-cloud';
var icon1=document.createElement('i');
icon1.className='fa-fa-car';
附加(图标);
anchor.append(icon1);
icon1.addEventListener('click',函数(e){
e、 预防默认值();
console.log('car clicked');
});
}
下载文件(“SampleData_12_07_2017_00_08_00.xlsx”);
当我对代码的生产版本使用类似的模式时,每次我都会得到“download=undefined”。虽然我可以下载文件名为“undefined.xlsx”的文件
我试图用anchor.setAttribute('download','')设置download,但在生产代码中它仍然设置为undefined。未指定任何值,不放置下载
属性
chrome和Firefox是否可能将其设置为未定义?那么,您向我们展示了有效的代码,然后让我们想象不同的代码可能有效,也可能无效?@Mike McCaughan浏览器对文件名是否有任何限制?