Javascript导出本地存储
我在一个网站上有这段代码,它将本地存储的内容导出为JSON格式的文件。Javascript导出本地存储,javascript,json,download,export,local-storage,Javascript,Json,Download,Export,Local Storage,我在一个网站上有这段代码,它将本地存储的内容导出为JSON格式的文件。 由于某种原因,它停止了工作。我在多个浏览器中进行了测试,但都是一样的。。。 不会显示任何错误,但也不会导出。 不同的变量看起来很好,但却无法导出。 老实说,我不知道如何做这一点不同,所以任何帮助将不胜感激 Thx 函数exportHistory(){ console.log(“启动”); var _myArray=JSON.stringify(localStorage,null,4);//JSON格式的缩进,可读 var v
由于某种原因,它停止了工作。我在多个浏览器中进行了测试,但都是一样的。。。 不会显示任何错误,但也不会导出。
不同的变量看起来很好,但却无法导出。
老实说,我不知道如何做这一点不同,所以任何帮助将不胜感激 Thx
函数exportHistory(){
console.log(“启动”);
var _myArray=JSON.stringify(localStorage,null,4);//JSON格式的缩进,可读
var vLink=document.getElementById('exportHistory'),
var vBlob=new Blob([\u myArray],{type:“octet/stream”}),
vName='working_history_uuz'+todayDate()+'.json',
vUrl=window.URL.createObjectURL(vBlob);
控制台日志(vLink);
vLink.setAttribute('href',vUrl);
setAttribute('download',vName);
控制台日志(“完成”);
}
出口历史
在这里,您需要将下载
属性添加到锚定标记
检查是否删除并用分号替换语句末尾的逗号有助于解决问题。。。顺便说一句,当查看控制台时,vLink变量包含以下内容:导出历史记录
此功能正常。你也知道为什么一个“鬼”链接可以工作,而一个按钮却不行吗?为什么它停止工作了?因为它以前工作过…可能是因为安全原因chrome删除了它。值得一看。
function exportHistory() {
console.log("started");
var _myArray = JSON.stringify(localStorage , null, 4); //indentation in json format, human readable
var vLink = document.getElementById('exportHistory'),
var vBlob = new Blob([_myArray], {type: "octet/stream"}),
vName = 'working_history_' + todayDate() + '.json',
vUrl = window.URL.createObjectURL(vBlob);
console.log(vLink);
vLink.setAttribute('href', vUrl);
vLink.setAttribute('download', vName );
console.log("finished");
}
<button class="btn btn-outline-secondary btn-sm" id="exportHistory" onclick="exportHistory()">Export History</button >
function exportHistory() {
console.log("started");
var _myArray = JSON.stringify(localStorage , null, 4); //indentation in json format, human readable
//Note: We use the anchor tag here instead button.
var vLink = document.getElementById('exportHistoryLink');
var vBlob = new Blob([_myArray], {type: "octet/stream"});
vName = 'working_history_' + todayDate() + '.json';
vUrl = window.URL.createObjectURL(vBlob);
console.log(vLink);
vLink.setAttribute('href', vUrl);
vLink.setAttribute('download', vName );
//Note: Programmatically click the link to download the file
vLink.click();
console.log("finished");
}
<button class="btn btn-outline-secondary btn-sm" id="exportHistory" onclick="exportHistory()">Export History</button >
<a id="exportHistoryLink" style="display: none;">Export</a>