【问题标题】:chrome.filesystem save file without prompt locationchrome.filesystem 保存文件没有提示位置
【发布时间】:2015-04-27 11:38:28
【问题描述】:

我可以将文件保存在自定义位置 (/home/Users/user1/),名称为 file1.txt

我有这个代码:

chrome.fileSystem.chooseEntry({type:'openDirectory'}, function(entry) {
    chrome.fileSystem.getWritableEntry(entry, function(entry) {
        entry.getFile('file1.txt', {create:true}, function(entry) {
            entry.createWriter(function(writer) {
                writer.write(new Blob(['Lorem'], {type: 'text/plain'}));
            });
        });
    });
});

使用此代码,我得到提示,因此我需要选择目录,但我想没有它,我想在设置中声明目录位置。

有什么解决办法吗?

编辑

根据@Xan 接受的回答:

// set location
$('#location_field').on('click', function(){
    chrome.fileSystem.chooseEntry({type:'openDirectory'}, function(entry) {
        chrome.storage.sync.set({'print_location': chrome.fileSystem.retainEntry(entry)});
    });
});

// get location 
var print_location = null;
chrome.storage.sync.get({
    print_location: null
}, function(items){
    chrome.fileSystem.restoreEntry(items.print_location, function(entry){
        print_location = entry;
    });
});

// save file
chrome.fileSystem.getWritableEntry(print_location, function(entry) {
    entry.getFile('file1.txt', {create:true}, function(entry) {
        entry.createWriter(function(writer) {
            writer.write(new Blob(['Lorem'], {type: 'text/plain'}));
        });
    });
});

【问题讨论】:

  • 不相关,但您可能不应该将所有参数都命名为 entry
  • 我同意,这是我找到的例子,所以我用它来检查它是否有效..
  • 快速请求:您能否将您的解决方案移到单独的答案中?保持问答形式会很有帮助(这样你会得到额外的代表!)

标签: html google-chrome google-chrome-extension google-chrome-app html5-filesystem


【解决方案1】:

没有。您不能预先选择可读/可写的路径 - 它始终必须通过用户确认才能获得 Entry 对象。将其视为一项安全功能。

但是,如果你声明了“retainEntries”子权限,你只能请求一次,然后再重复使用该条目。

请参阅 retainEntryrestoreEntry 的文档。

此外,如果您知道理想的位置,您可以尝试为chooseEntry 提供suggestedName。不过,我不确定如果您提供绝对路径,它是否会起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    相关资源
    最近更新 更多