我有以下代码,让用户下载数据字符串在csv文件。
exportData = 'data:text/csv;charset=utf-8,';
exportData += 'some csv strings';
encodedUri = encodeURI(exportData);
newWindow = window.open(encodedUri);
它工作得很好,如果客户端运行代码,它会生成空白页,并开始下载csv文件中的数据。
我试着用JSON对象来做这个
exportData = 'data:text/json;charset=utf-8,';
exportData += escape(JSON.stringify(jsonObject));
encodedUri = encodeURI(exportData);
newWindow = window.open(encodedUri);
但是我只看到一个页面,上面显示了JSON数据,没有下载它。
我进行了一些研究,这一个声称工作,但我没有看到我的代码有任何不同。
我在代码中遗漏了什么吗?
谢谢你阅读我的问题。
2021年的ES6+版本;也没有1MB限制:
这是从@maia的版本改编而来的,针对现代Javascript进行了更新,将废弃的initMouseEvent替换为新的MouseEvent(),代码总体上得到了改进:
const saveTemplateAsFile = (filename, dataObjToWrite) => {
const blob = new Blob([JSON.stringify(dataObjToWrite)], { type: "text/json" });
const link = document.createElement("a");
link.download = filename;
link.href = window.URL.createObjectURL(blob);
link.dataset.downloadurl = ["text/json", link.download, link.href].join(":");
const evt = new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: true,
});
link.dispatchEvent(evt);
link.remove()
};
如果你想传递一个对象:
saveTemplateAsFile("filename.json", myDataObj);
这是我如何解决我的应用程序:
。.HTML:
<a id=“downloadAnchorElem” style=“display:none”></a>
JS(纯JS,这里不是jQuery):
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(storageObj));
var dlAnchorElem = document.getElementById('downloadAnchorElem');
dlAnchorElem.setAttribute("href", dataStr );
dlAnchorElem.setAttribute("download", "scene.json");
dlAnchorElem.click();
在这种情况下,storageObj是你想要存储的js对象,而“scene。Json”只是结果文件的一个示例名称。
与其他建议的方法相比,该方法具有以下优点:
不需要单击HTML元素
结果将被命名为您想要的
不需要jQuery
我需要这种行为没有显式点击,因为我想在某个时候从js自动触发下载。
JS解决方案(不需要HTML):
function downloadObjectAsJson(exportObj, exportName){
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", exportName + ".json");
document.body.appendChild(downloadAnchorNode); // required for firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
}
下面的方法对我很有效:
/* function to save JSON to file from browser
* adapted from http://bgrins.github.io/devtools-snippets/#console-save
* @param {Object} data -- json object to save
* @param {String} file -- file name to save to
*/
function saveJSON(data, filename){
if(!data) {
console.error('No data')
return;
}
if(!filename) filename = 'console.json'
if(typeof data === "object"){
data = JSON.stringify(data, undefined, 4)
}
var blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')
a.download = filename
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
a.dispatchEvent(e)
}
然后这样称呼它
saveJSON(myJsonObject, "saved_data.json");