Incase anybody else is needed to do this, after a a lot of reading stackoverflow I learnt:
Here is the full code
Code: Select all
//https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/#writeFile
//https://stackoverflow.com/questions/12748920/simple-example-with-window-requestfilesystem-function/12750638
const _that = this;
function writeFile(fileEntry, str) {
// Create a FileWriter object for our FileEntry
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function(res) {
console.log("writeFile OK: ", res);
//https://ionicframework.com/docs/native/file-opener
_that.fileOpener.open(res.target.localURL, 'application/vnd.google-earth.kml+xml')
.then((res) => console.debug('fileOpener.open OK: ', res))
.catch(e => console.log('fileOpener.open Error:', e));
};
fileWriter.onerror = function (e) {
console.log("Failed file write: " + e.toString());
};
const dataObj = new Blob([str], { type: 'text/plain' });
fileWriter.write(dataObj);
});
}
function createFile(dirEntry, fileName, str) {
// Creates a new file or returns the file if it already exists.
dirEntry.getFile(fileName, {create: true, exclusive: false},
function(fileEntry) {
console.debug('createFile: ', fileEntry);
writeFile(fileEntry, str);
},
onErrorCreateFile);
}
let onErrorCreateFile = (err) => console.error(err);
let onErrorLoadFs = (err) => console.error(err);
const window:any = globalThis.window; // Find window in Angular??
//https://stackoverflow.com/questions/6803064/window-requestfilesystem-not-working
//TEMPORARY or PERSISTENT
const requestFS = window.requestFileSystem || window.webkitRequestFileSystem;// depends on which platfrom we run on
requestFS(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {
console.debug('requestFileSystem: ', fs);
createFile(fs.root, "track.kml", KMLstr);
}, onErrorLoadFs);
console.groupEnd();