save and change the name of the file with chrome.f

2019-09-04 12:25发布

问题:

Is this possible with chrome.fileSystem to save a file on the client's disk, but by changing the file name. For example, I compress a file and I want that file is stored on the disc with my new name newFile.min.js for example. I can save the file to disk, but not to change its name. And what I would like is that everything is done in a "silent" without a window that asks for the location.

Here is the function I use to save my files:

    function saveToEntry(entry, result) {
  setTitle();

  var blob = new Blob([result], {type: 'text/plain'});
  currentEntry.createWriter(function(writer) {
    writer.onwrite = function() {
      writer.onwrite = null;
      writer.write(blob);
    }
    writer.truncate(blob.size);
  });
}

function setTitle() {
  chrome.fileSystem.getDisplayPath(
      currentEntry,
      function(path) {
console.log(path);
        document.title = path;
      });
}

Is this possible? And if so, if you have any example to guide me would be great. If not, would be great too :)

Thank you in advance for your advice!

回答1:

For that (saving in a separate file), I'm afraid that you need to ask the user for write access for a folder, not an individual file. Then you will be able to create a new file in it.

I suggest that you either ask the user to select a "working" directory, present an in-app file picker from that folder, and save the result next to the normal file.

Alternatively, ask the user to select an "output" folder, and dump the results there.


Another solution would be to require a Native Host module. You'll be able to access the system with the same rights as the user. It will limit your deployment options though: you'll need a separate installer for your module that can't be hosted on Chrome Web Store.