Replace blob file in browser?

I've asked this before, but someone voted and accused me of trying to write files to the local filesystem without reading.

I have a website in a corporate environment that will only ever be accessed in chrome, so keep that in mind.

I can select a local folder on my PC and open all subfolders and files in the browser. I am using client side javascript to parse these files and look for a specific kind of XML file that is used internally to render PowerPoint as a presentation. I can make changes to this xml file and spit it back out as a blob.

What I would like to do but do not know how to replace the data or blob in the original file with the changed data / blob.

+3


source to share


1 answer


Can the user interact with the data block? If so, you can use the 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 elsewhere on Stack Overflow. I can't remember who it is because of this to provide attribution :-(

+2


source







All Articles