function download(){
var fileName = 'testDemo';
var type = 'xyz';
var data = 'Hello world';
var file = new Blob([data], {
type: type
});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, fileName);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = fileName + '.' +type;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 100);
}
}

<button onclick='download()'>Download</button>
&#13;
此处我将类型和扩展名设置为.xyz
,但仍然在属性中显示plain text document (text/plain)
,并且无法读取显示空字符串的文件类型。如何创建自定义扩展并输入?
答案 0 :(得分:1)
在实例化blob对象时,您正在传递扩展名而不是有用的mime类型。
var fileName = 'testDemo';
var type = 'application/pdf';
var data = 'Hello world';
var file = new Blob([data], {
type: type
});
在此处参考其他有用的mime类型: mime types