在Chrome应用中保存没有保存对话框的文件 [英] Save a file without the save dialog box in Chrome Apps

查看:131
本文介绍了在Chrome应用中保存没有保存对话框的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Chrome应用程序中,我正在将一些数据写入文件。
我使用存储API 下的官方网站提供的以下代码 p>

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

现在我需要避免弹出保存对话框,并将该文件保存在预定义位置。

这可能吗?

解决方案

如果您想要持久数据但不想打扰用户使用本地计算机上的数据位置,您有以下几种选择:

$ ul

  • 本地存储
  • IndexedDB

  • HTML5档案API

  • chrome.storage

  • chrome.storage.sync

  • chrome.syncFileSystem



  • 这些都不会给你用户可以访问的真实文件。如果用户(除了您自己的应用程序)需要能够访问您编写的文件,则需要向用户询问文件的放置位置。正如Cerbrus在对你的问题的评论中所说的那样,在用户机器上静静地放置一个文件会是一个安全问题。



    Chrome 31的一项新功能是 chrome.fileSystem.retainEntry 。使用这个API,您的应用程序将保留在整个应用程序重新启动过程中保留大量文件条目(包括目录)的能力,这样您就不必一直在窃听用户选择文件位置。我的理解是,用户被允许在哪些位置选择是有限制的,因此恶意应用无法指示用户指示Chrome覆盖系统文件。


    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?

    解决方案

    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:

    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.

    这篇关于在Chrome应用中保存没有保存对话框的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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