Store chrome.filesystem file uninvited
Is it possible to save the file to a custom location ( /home/Users/user1/
) named 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 a hint, so I need to select a directory, but I want without that, I want to declare the directory location in the settings.
Any solution for this?
EDIT
As per the 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'}));
});
});
});
source to share
Not. You cannot preselect the read / write path - it always has to go through user confirmation to get the Entry object. Consider it as a security feature.
However, if you declare the "preserveEntries" sub-permission, you can only request one time and then reuse the entry.
See documentation for retainEntry
and restoreEntry
.
Alternatively, you can try to provide suggestedName
for chooseEntry
if you know where you want it to be perfect. I'm not sure if this will work if you provide an absolute path.
source to share