保存从浏览器创建的文件到磁盘 - 无法在FireFox上使用新的File() [英] Saving file created from browser to disk - Cannot use new File() on FireFox
问题描述
它可以很好地与 Chrome 50.0.2661.87
m,但它在 FF 47.0a2
上不起作用(尽管不会有错误发生)。
$ b
问题似乎与(如果您将其注释掉):
window.URL .revokeObjectURL(link.href);
这种行为在FF上的原因是什么?我应该省略window.URL.revokeObjectURL();
注意:它应该在最新的浏览器Chrome和FireFox上运行。
var saveDataToFile = function(data,fileName,properties){window.URL = window.URL || window.webkitURL; var file = new File(data,fileName,properties),link = document.createElement('a'); link.href = window.URL.createObjectURL(file); link.download = fileName; document.body.appendChild(链接); link.click(); window.URL.revokeObjectURL(link.href); //可能存在的问题document.body.removeChild(link)}; var fileName ='test.json',properties = {type:'octet / stream'},data = [ JSON.stringify({test:'hello'})]; saveDataToFile(data,fileName,properties);
删除此行
window.URL .revokeObjectURL(link.href);
看起来好像是在将url用于下载之前。
测试: https://jsfiddle.net/cLvf7yny/
The following script creates a json file in the browser and automatically save it to disk.
It works fine with Chrome 50.0.2661.87
m but it does not work on FF 47.0a2
(although no errors are throw).
Issue seems related to (as if you comment it out works):
window.URL.revokeObjectURL(link.href);
What is the reasons for this behavior on FF? Should I omit window.URL.revokeObjectURL() at all;
Notes: It should works on the latest browsers Chrome and FireFox.
var saveDataToFile = function(data, fileName, properties) {
window.URL = window.URL || window.webkitURL;
var file = new File(data, fileName, properties),
link = document.createElement('a');
link.href = window.URL.createObjectURL(file);
link.download = fileName;
document.body.appendChild(link);
link.click();
window.URL.revokeObjectURL(link.href);// possible problem here
document.body.removeChild(link)
};
var fileName = 'test.json',
properties = {
type: 'octet/stream'
},
data = [JSON.stringify({
test: 'hello'
})];
saveDataToFile(data, fileName, properties);
Remove this line
window.URL.revokeObjectURL(link.href);
Seems like you are disposing url before it is used for download.
Test: https://jsfiddle.net/cLvf7yny/
这篇关于保存从浏览器创建的文件到磁盘 - 无法在FireFox上使用新的File()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!