Save a file without the save dialog box in Chrome Apps

人走茶凉 提交于 2019-12-11 06:55:46

问题


In my chrome app, I am writing some data to a file. I used the following code available in the official website under Storage APIs

chrome.fileSystem.chooseEntry({type: 'saveFile'}, function(writableFileEntry) {
writableFileEntry.createWriter(function(writer) {
  writer.onerror = errorHandler;
  writer.onwriteend = function(e) {
    console.log('write complete');
  };
  writer.write(new Blob(['1234567890'], {type: 'text/plain'}));  
}, errorHandler);
});

Now I need to avoid popping up the save dialog box, and save this file in a predefined location.

Is this possible to do?


回答1:


If you want persistent data but don't want to bother the user with the location of the data on the local machine, you have several options:

  • Local storage
  • IndexedDB
  • HTML5 file API
  • chrome.storage
  • chrome.storage.sync
  • chrome.syncFileSystem

None of these will give you real files that the user can access. If the user (in addition to your own app) needs to be able to access the file that you've written, then the user needs to be asked where to put the file. As Cerbrus said in the comment to your question, silently putting a file somewhere on the user's machine would be a security issue.

A new feature in Chrome 31 is chrome.fileSystem.retainEntry. Using this API, your app will keep the ability to retain a number of fileEntries, including directories, across app restarts, so that you won't have to keep bugging the user to choose the file location. It's my understanding that there are restrictions in which locations the user is allowed to pick, so that a malicious app can't direct the user to instruct Chrome to overwrite system files.



来源:https://stackoverflow.com/questions/20836604/save-a-file-without-the-save-dialog-box-in-chrome-apps

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