chrome.filesystem 没有提示位置的保存文件
chrome.filesystem save file without prompt location
我可以将文件保存在自定义位置 (/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'}));
});
});
});
没有。您不能预先 select 路径成为 readable/writable - 它总是必须经过用户确认才能获得 Entry 对象。将其视为一项安全功能。
但是,如果您声明了 "retainEntries" 子权限,您可以只询问一次然后重新使用该条目。
请参阅 retainEntry
and restoreEntry
的文档。
此外,您可以尝试为 chooseEntry
提供 suggestedName
,如果您知道理想情况下它的位置。如果您提供绝对路径,我不确定它是否有效。
我可以将文件保存在自定义位置 (/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'}));
});
});
});
没有。您不能预先 select 路径成为 readable/writable - 它总是必须经过用户确认才能获得 Entry 对象。将其视为一项安全功能。
但是,如果您声明了 "retainEntries" 子权限,您可以只询问一次然后重新使用该条目。
请参阅 retainEntry
and restoreEntry
的文档。
此外,您可以尝试为 chooseEntry
提供 suggestedName
,如果您知道理想情况下它的位置。如果您提供绝对路径,我不确定它是否有效。