[英]Replace file blob in browser?
我早些時候問了這個,但是有人拒絕投票並指責我試圖在沒有閱讀的情況下將文件寫入本地文件系統。
我在企業環境中擁有只能以chrome訪問的網站,所以請記住這一點。
我可以在我的電腦上選擇一個本地文件夾,並在瀏覽器中打開所有子文件夾和文件。 我正在使用客戶端javascript來解析這些文件,並查找一種特定類型的.xml文件,該文件在內部用於呈現像演示文稿一樣的powerpoint。 我可以對這個xml文件進行更改並將其作為blob吐出。
我想做什么,但不知道如何用修改后的數據/ blob替換原始文件中的數據或blob。
用戶可以與數據blob進行交互嗎? 如果是這樣,您可以使用保存文件功能並覆蓋原始文件。
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?")
}
}
我從堆棧溢出的其他地方抓取了這段代碼。 我不記得是誰提供歸因:-(
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.