Pergunta

I am working on a Chrome Extension where in the options.html page the user should be able to upload an audio file. It seems that I am not allowed to use PHP when writing a Chrome extension, so what I'm wondering is how to save the audio file with just JQuery.

What I have:

HTML

<form >
   <input type="file" id="upload" name="snd" />
   <input type="submit" id="submit" value="Upload" />
</form>

JS

$('#submit').on('click', (function(e) {
    upload = document.getElementById("upload").files[0];
    return false;
}))

This fetches the file, but now I don't know how to save it. Please help!

Thank you!

Foi útil?

Solução

Chrome allows file system access only within a sandboxes environment. See the following example for going about accessing it:

document.querySelector('#upload').onchange = function(e) {
var files = this.files;

window.requestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
// Duplicate each file the user selected to the app's fs.
for (var i = 0, file; file = files[i]; ++i) {

  // Capture current iteration's file in local scope for the getFile() callback.
  (function(f) {
    fs.root.getFile(f.name, {create: true, exclusive: true}, function(fileEntry) {
      fileEntry.createWriter(function(fileWriter) {
        fileWriter.write(f); // Note: write() can take a File or Blob object.
      }, errorHandler);
    }, errorHandler);
  })(file);

}
}, errorHandler);
};

Here's a good resource for further info:

http://www.html5rocks.com/en/tutorials/file/filesystem/

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top