如何使用JavaScript下载文件

How to download file using JavaScript

本文关键字:文件 下载 JavaScript 何使用      更新时间:2023-09-26

如何仅使用JavaScript 下载文件?

请不要给我这样的回答

"Change the MIME in the sever"

window.open( URL ,"_blank")

因为我知道还有别的解。谢谢。

编辑:


我要下载的文件是.JSON文件

您可以动态创建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';

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

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