Skip to content

Instantly share code, notes, and snippets.

@taufiksu
Last active July 20, 2017 09:48
Show Gist options
  • Save taufiksu/e286a438dc0725ad60a0f1d49cedd1f6 to your computer and use it in GitHub Desktop.
Save taufiksu/e286a438dc0725ad60a0f1d49cedd1f6 to your computer and use it in GitHub Desktop.
Cordova function for download file and store into public folder
function fileDownload(dirName, fileURL, fileName) {
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsAccess, fsFail);
function fsAccess(fileSystem) {
fileSystem.root.getDirectory(dirName, { create: true, exclusive: false }, dirReady, dirFail);
}
function fsFail() {
console.log("Filesystem Access Failed");
}
function dirFail() {
console.log("Directory Access Failed");
}
function dirReady(entry) {
window.appRootDir = entry;
console.log(JSON.stringify(window.appRootDir));
var fileTransfer = new FileTransfer();
fileTransfer.download(
fileURL,
window.appRootDir.nativeURL + fileName,
function (theFile) {
console.log("download complete: " + theFile.toURL());
},
function (error) {
console.log(JSON.stringify(error));
}
);
}
}
@taufiksu
Copy link
Author

taufiksu commented Jul 20, 2017

// add code to config.xml

<preference name="AndroidPersistentFileLocation" value="Compatibility" />

// To Use It

document.getElementById("download").onclick = function () {
    fileDownload('Research', 'http://www.inkwelleditorial.com/pdfSample.pdf', 'myfile.pdf');
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment