如何使用JavaScript下载文件

Der*_*會功夫 -3 javascript google-chrome-extension

如何使用JavaScript下载文件?

请不要给我答案

"Change theMIMEin the sever"

要么

window.open(URL,"_blank")

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

编辑:
我要下载的.JSON文件是一个文件.

Mic*_*ski 9

您可以动态创建iframe并将其位置设置为您要下载的URL.

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

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

document.getElementById("theFrame").src = 'http://www.example.com/yourfile.doc';
Run Code Online (Sandbox Code Playgroud)