如何使用JavaScript下载文件

时间:2011-08-03 01:21:08

标签: javascript google-chrome-extension

如何使用JavaScript 下载文件?

请不要给我回答

"Change the MIME in the sever"

window.open( URL ,"_blank")

因为我知道还有其他解决方案。感谢。

修改
我要下载的文件是.JSON文件。

1 个答案:

答案 0 :(得分:9)

您可以动态创建iframe并将其位置设置为您要下载的网址。

var f = document.createElement("iframe");
f.setAttribute("id", "theFrame");
document.body.appendChild(f);
document.getElementById("theFrame").location = 'http://www.example.com/yourfile.doc';

不确定上述是否正常。如果没有,请尝试在最后一行src而不是location进行设置:

document.getElementById("theFrame").src = 'http://www.example.com/yourfile.doc';
相关问题