【发布时间】: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