Skip to content

Instantly share code, notes, and snippets.

@JaosnHsieh
Created January 4, 2022 10:03
Show Gist options
  • Save JaosnHsieh/95f2826b7c439acc40da5cee8fe7d97f to your computer and use it in GitHub Desktop.
Save JaosnHsieh/95f2826b7c439acc40da5cee8fe7d97f to your computer and use it in GitHub Desktop.
cordova-file-plugin write file example works on android 9
// from https://forum.quasar-framework.org/topic/3375/solved-v1-download-a-file-in-cordova-app/2
window.test = {};
window.test.writeFile = (fileName, content) => {
return new Promise((ok, fail) => {
//@ts-ignore
if (!window.requestFileSystem) {
console.error(`$ window.requestFileSystem is not available`);
fail(false);
return;
}
//@ts-ignore
const dirPath = cordova.file.externalRootDirectory + 'Download';
const fullPath = dirPath + '/' + fileName;
console.log(`$ writeFile fullPath`, fullPath);
//@ts-ignore
window.resolveLocalFileSystemURL(
//@ts-ignore
dirPath,
fs => {
console.log('file system open: ' + fs.name);
fs.getFile(fileName, { create: true, exclusive: false }, function(fileEntry) {
console.log('$ fileEntry is file?' + fileEntry.isFile.toString());
// fileEntry.name == 'someFile.txt'
// fileEntry.fullPath == '/someFile.txt'
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function() {
console.log('$ Successful file write... fileEntry.fullPath', fileEntry.fullPath);
ok(fileEntry.fullPath);
// readFile(fileEntry);
};
fileWriter.onerror = function(e) {
console.error('$ Failed file write: ' + e.toString());
};
fileWriter.write(new Blob([content], { type: 'text/plain' }));
});
});
},
e => {
console.error(`$ window.resolveLocalFileSystemURL e`, e);
fail('');
},
);
});
};
window.test.writeFile("123-file","test")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment