Chrome 应用程序:如何在后台将 blob 内容保存到文件系统?
Chrome Apps : How to save blob content to fileSystem in the background?
在 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 仅受 Kiosk-only 应用程序支持。
因此,我使用 HTML5 文件系统 API 而不是 chrome。
我正在使用以下代码使 XHR 获取 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 方法
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
为 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.
解决这个问题的方法是什么?
我试过在启动应用程序时使用 --allow-file-access-from-files
标记。没用。
Chrome 应用程序的沙箱存储不允许将文件存储在根目录中(即 /
)
修改代码保存在特定的sub-directory下吧。
例如 -
fileSystem.writeBlob("/new"+response.name, response).then(function() {
console.log("file saved");
}, function(err) {
console.log(err);
});
这将成功地将文件保存在 /new/
目录下。
为了对此进行扩展,这里有一个完整的示例应用程序,说明如何下载文件并保存 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 仅受 Kiosk-only 应用程序支持。
因此,我使用 HTML5 文件系统 API 而不是 chrome。
我正在使用以下代码使 XHR 获取 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 方法
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
为 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.
解决这个问题的方法是什么?
我试过在启动应用程序时使用 --allow-file-access-from-files
标记。没用。
Chrome 应用程序的沙箱存储不允许将文件存储在根目录中(即 /
)
修改代码保存在特定的sub-directory下吧。 例如 -
fileSystem.writeBlob("/new"+response.name, response).then(function() {
console.log("file saved");
}, function(err) {
console.log(err);
});
这将成功地将文件保存在 /new/
目录下。
为了对此进行扩展,这里有一个完整的示例应用程序,说明如何下载文件并保存 blob 并将其显示给用户。