Save without prompt in a Chrome Packaged App

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 19:01:10

I had some conversations with people involved in the packaged apps development and as it is today you cant have access to the file system without prompt our outside of an apps sandbox. But it might come in the future.

But there is a new project called Node-Webkit that is very similar to Chrome packaged apps where you develop your app in Chrome with HTML, js etc where you also can run nodejs in the browser. Using this you can access the file system and many more things.

Check it out here: https://github.com/rogerwang/node-webkit and https://github.com/rogerwang/node-webkit/wiki

Loading and saving files in Node-Webkit is very easy. It looks like this

var gui = require("nw.gui");
var fs = require("fs");

//Save
fs.writeFile('message.txt', 'Hello Node', function (err) {
    if (err) throw err;
    console.log('It\'s saved!');
});

//Load
fs.readFile('message2.txt', 'utf8', function (err, data) {
    if (err) throw err;
    console.log(data);
});

It looks like you will soon be able to do this -- see https://groups.google.com/a/chromium.org/forum/#!topic/apps-dev/fu1TyjdYLEc

Kind of. You can save to your app's "workspace", which means the file will be saved, but the user or other apps won't have access to it. It will be in a sandboxed storage area. To do it, just use the HTML5 Filesystem API (tutorial here).

You need: "permissions": ["storage"] in your manifest, and in your js you can do:

var onInitFsCallback=function(fs) {
  fs.root.getFile('log.txt', {create: true}, function(fileEntry) {
    fileEntry.createWriter(function(fileWriter) {
      _this.fileWriter = fileWriter;
      fileWriter.onwriteend = onWriteEndCallback;
      fileWriter.onerror = onErrorCallback;
      fileWriter.write(myBlob);
      fileWriter.write(myString);
    }, errorHandler);

  }, errorHandler);

};
window.webkitRequestFileSystem([window.TEMPORARY or window.PERMANENT], sizeInBytes,
  onInitFsCallback, onErrorCallback);

You can also inspect your app's filesystem with Inspector Tools (Resources -> Filesystem). From there, you can either download or delete files. In Chrome 24, you need to Enable Developer Tools experiments in chrome://flags, click on the DevTools settings menu, Experiments, and enable the FileSystem inspection.

On the other hand, if you want to save in an arbitrary location outside of your app's sandbox, the user prompt is required for security reasons.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!