Chrome应用:如何BLOB内容保存到文件系统中的背景是什么? [英] Chrome Apps : How to save blob content to fileSystem in the background?

查看:2479
本文介绍了Chrome应用:如何BLOB内容保存到文件系统中的背景是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Chrome应用,我从服务器下载使用JavaScript XHR一个blob内容(角$ HTTP GET特别是与反应型'一滴')

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

我应如何保存到Chrome浏览器的应用程序的文件系统?

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

目前使用HTML5的文件系统API的角度包装
https://github.com/maciel310/angular-filesystem

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

我不希望显示用户弹出(所以我不能使用 chrome.fileSystem。chooseEntry

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

chrome.fileSystem.requestFileSystem API仅由亭,仅应用支持。
因此,我使用的是Chrome的HTML5的文件系统API来代替。

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

我用下面的code键使XHR获取BLOB

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) {

      });

这是我的writeBlob方法

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;
},

本节目 SECURITY_ERR 已经确定,某些文件是不安全的Web应用中的访问,或过多的呼叫正在作出的文件资源。

什么是解决此?

我试过使用 - 允许文件访问 - 从 - 文件标志同时发动应用。它并不能帮助。

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

推荐答案

Chrome浏览器应用程序的沙盒存储不允许的文件存储在根目录下(即 /

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

修改code将其保存在一个特定的子目录下它。
例如 -

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.

这篇关于Chrome应用:如何BLOB内容保存到文件系统中的背景是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆