在Chrome Apps中,我正在使用JavaScript XHR(尤其是 Angular $ http GET,响应类型为“blob”)从服务器下载blob内容
如何将其保存到chrome应用程序的文件系统中?
当前在HTML5文件系统API上使用Angular包装器
https://github.com/maciel310/angular-filesystem
我不想向用户显示弹出窗口(因此我不能使用chrome.fileSystem. chooseEntry
)
仅Kiosk专用应用程序支持chrome.fileSystem.requestFileSystem
API。
因此,我使用的是HTML5 FileSystem 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应用程序的沙箱存储不允许将文件存储在根目录(即/
)中
修改代码以将其保存在其下面的特定子目录中。
例如 -
fileSystem.writeBlob("/new"+response.name, response).then(function() {
console.log("file saved");
}, function(err) {
console.log(err);
});
这样可以成功将文件保存在
/new/
目录下。