chrome.filesystem保存没有提示位置的文件 [英] chrome.filesystem save file without prompt location

查看:268
本文介绍了chrome.filesystem保存没有提示位置的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我有这样的代码:

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

有了这段代码,我得到了提示,所以我需要选择目录,但是我想没有这个,我想要在设置中声明目录位置。



任何解决方案?

编辑



根据@Xan接受的答案:

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

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

//保存文件
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对象。不过,如果你声明了retainEntries子权限,那么你只需要询问一次,然后重用该条目。

$ / $> $ / $> b
$ b

请参阅 retainEntry restoreEntry



另外,您可以尝试为<$ c提供 suggestedName $ c> chooseEntry ,如果你知道你想要的理想位置。我不确定如果你提供了绝对路径,它是否会工作。


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

解决方案

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.

这篇关于chrome.filesystem保存没有提示位置的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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