chrome.filesystem save file without prompt location

依然范特西╮ 提交于 2019-12-01 07:15:28

问题


can i save file in custom location (/home/Users/user1/) with name file1.txt.

I have this code:

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'}));
            });
        });
    });
});

With this code, i get prompt so i need to choose directory, but i want without that, i wanna declare directory location in settings.

Any solution for this?

EDIT

According to accepted answer from @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'}));
        });
    });
});

回答1:


No. You cannot pre-select a path to be readable/writable - it always has to go through a user confirmation to gain the Entry object. Consider it a security feature.

However, if you declare the "retainEntries" sub-permission, you can ask only once and then reuse the entry.

See documentation for retainEntry and restoreEntry.

Also, you may try to provide a suggestedName for chooseEntry if you know where you want it to ideally be. I'm not sure if it will work if you provide an absolute path though.



来源:https://stackoverflow.com/questions/29894660/chrome-filesystem-save-file-without-prompt-location

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