Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

javascript - Chrome Apps : How to save blob content to fileSystem in the background?

In Chrome Apps, I'm downloading a blob content from a server using JavaScript XHR (Angular $http GET in particular, with response type 'blob')

How should I save this to chrome application's file system?

Currently using an Angular wrapper on HTML5 filesystem API https://github.com/maciel310/angular-filesystem

I do not want to show user a popup (hence I can't use chrome.fileSystem. chooseEntry )

The chrome.fileSystem.requestFileSystem API is only supported by Kiosk-only apps. Hence I'm using HTML5 FileSystem API instead of chrome's.

I'm using following code to make XHR to fetch blob.

 $http({
          url: SERVER_URL+"/someVideo.mp4",
          method: "GET",
          responseType: "blob"
      }).then(function(response) {
          console.log(response);
          fileSystem.writeBlob(response.name, response).then(function() {
             console.log("file saved");
          }, function(err) {
              console.log(err);
          });
      }, function (response) {

      });

This is my writeBlob method

writeBlob: function(fileName, blob, append) {
    append = (typeof append == 'undefined' ? false : append);

    var def = $q.defer();

    fsDefer.promise.then(function(fs) {

        fs.root.getFile(fileName, {create: true}, function(fileEntry) {

            fileEntry.createWriter(function(fileWriter) {
                if(append) {
                    fileWriter.seek(fileWriter.length);
                }

                var truncated = false;
                fileWriter.onwriteend = function(e) {
                    //truncate all data after current position
                    if (!truncated) {
                        truncated = true;
                        this.truncate(this.position);
                        return;
                    }
                    safeResolve(def, "");
                };

                fileWriter.onerror = function(e) {
                    safeReject(def, {text: 'Write failed', obj: e});
                };

                fileWriter.write(blob);

            }, function(e) {
                safeReject(def, {text: "Error creating file", obj: e});
            });

        }, function(e) {
            safeReject(def, {text: "Error getting file", obj: e});
        });

    }, function(err) {
        def.reject(err);
    });

    return def.promise;
},

This shows SECURITY_ERR as It was determined that certain files are unsafe for access within a Web application, or that too many calls are being made on file resources.

What's the solution for this?

I've tried using --allow-file-access-from-files flag while launching app. It doesn't help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Chrome Application's sandbox storage doesn't allow files to be stored in root directory (i.e. / )

Modify the code to save it in a specific sub-directory under it. For example -

fileSystem.writeBlob("/new"+response.name, response).then(function() {
             console.log("file saved");
          }, function(err) {
              console.log(err);
          });

This would successfully save the file under /new/ directory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...