【发布时间】:2014-01-09 09:34:28
【问题描述】:
我希望我页面的用户能够保存和加载包含图像和数值的数据(单个文件)。
我找到了保存图片的方法
saveImgAs = function (img, fileName) {
a = document.createElement('a');
a.setAttribute('href', img.src);
a.setAttribute("download", fileName);
a.click();
}
和文本文件
saveTextAsFile = function (textToWrite, fileNameToSaveAs) {
var textFileAsBlob = new Blob([textToWrite], { type: 'text/plain' });
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null) {
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
}
else {
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
};
destroyClickedElement = function(event) {
document.body.removeChild(event.target);
};
但我不知道如何将它们合并到一个文件以及如何重新加载该结构..
有什么想法吗?
【问题讨论】:
标签: javascript image load save blob