[英]Replace file blob in browser?
I asked this earlier but someone down voted and accused me of trying to write files to local filesystem without reading. 我早些时候问了这个,但是有人拒绝投票并指责我试图在没有阅读的情况下将文件写入本地文件系统。
I have website in an enterprise environment which will only ever be accessed in chrome so keep that in mind. 我在企业环境中拥有只能以chrome访问的网站,所以请记住这一点。
I am able to select a local folder on my PC and open all sub-folders and files in the browser. 我可以在我的电脑上选择一个本地文件夹,并在浏览器中打开所有子文件夹和文件。 I'm using client side javascript to parse these files and look for a particular kind of .xml file that is used internally to render a powerpoint like presentation.
我正在使用客户端javascript来解析这些文件,并查找一种特定类型的.xml文件,该文件在内部用于呈现像演示文稿一样的powerpoint。 I can make changes to this xml file and spit it back out as a blob.
我可以对这个xml文件进行更改并将其作为blob吐出。
What I would like to do but don't know how is replace the data or blob in the original file with the modified data/blob. 我想做什么,但不知道如何用修改后的数据/ blob替换原始文件中的数据或blob。
Can the user interact with the data blob? 用户可以与数据blob进行交互吗? If so, you can use a save file function and overwrite the original.
如果是这样,您可以使用保存文件功能并覆盖原始文件。
function saveFile( data )
{
var textFileAsBlob = new Blob([yourData], {type:'text/plain'});
//or replace the code above with your already formed blob
var fileNameToSaveAs = "File_Name_Goes_Here.xml";
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.
try {
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
catch( e ){
console.log("error saving firefox file")
}
// IE 10+
try {
window.navigator.msSaveBlob(textFileAsBlob, fileNameToSaveAs);
}
catch(e){
console.log("error saving IE file")
}
}
try {
downloadLink.click();
}
catch(e) {
console.log("Unable to click the download link. Are you using IE?")
}
}
I grabbed this code from somewhere else on stack overflow. 我从堆栈溢出的其他地方抓取了这段代码。 I can't remember who it's from though to provide attribution :-(
我不记得是谁提供归因:-(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.