Chrome 应用程序:如何在后台将 blob 内容保存到文件系统? [英] Chrome Apps : How to save blob content to fileSystem in the background?

查看:46
本文介绍了Chrome 应用程序:如何在后台将 blob 内容保存到文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Chrome 应用程序中,我正在使用 JavaScript XHR(特别是 Angular $http GET,响应类型为blob")从服务器下载 blob 内容

我应该如何将其保存到 Chrome 应用程序的文件系统?

目前在 HTML5 文件系统 API 上使用 Angular 包装器https://github.com/maciel310/angular-filesystem

我不想向用户显示弹出窗口(因此我不能使用 chrome.fileSystem.chooseEntry )

chrome.fileSystem.requestFileSystem API 仅支持自助服务终端应用.因此我使用的是 HTML5 FileSystem API 而不是 chrome 的.

我正在使用以下代码让 XHR 获取 blob.

 $http({网址:SERVER_URL+"/someVideo.mp4",方法:获取",响应类型:斑点"}).then(函数(响应){控制台日志(响应);fileSystem.writeBlob(response.name, response).then(function() {console.log("文件已保存");}, 函数(错误){控制台日志(错误);});}, 函数(响应){});

这是我的 writeBlob 方法

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) {如果(附加){fileWriter.seek(fileWriter.length);}var 截断 = false;fileWriter.onwriteend = 函数(e){//截断当前位置后的所有数据如果(!截断){截断 = 真;this.truncate(this.position);返回;}安全解决(定义,");};fileWriter.onerror = 函数(e){safeReject(def, {text: '写失败', obj: e});};fileWriter.write(blob);}, 函数(e) {safeReject(def, {text: "Error 创建文件", obj: e});});}, 函数(e) {安全拒绝(定义,{文本:获取文件时出错",对象:e});});}, 函数(错误){def.reject(err);});返回 def.promise;},

这将 SECURITY_ERR 显示为 已确定某些文件在 Web 应用程序中访问不安全,或者对文件资源进行了过多调用.>

有什么办法可以解决这个问题?

我尝试在启动应用程序时使用 --allow-file-access-from-files 标志.它没有帮助.

解决方案

Chrome 应用程序的沙盒存储不允许将文件存储在根目录(即 / )

修改代码保存在其下的特定子目录中.例如 -

fileSystem.writeBlob("/new"+response.name, response).then(function() {console.log("文件已保存");}, 函数(错误){控制台日志(错误);});

这将成功地将文件保存在 /new/ 目录下.

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.

解决方案

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.

这篇关于Chrome 应用程序:如何在后台将 blob 内容保存到文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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