How to download file to Download's directory with Ionic Framework?
10,332
Solution 1
cordova.file.externalRootDirectory + '/Download/' + 'sample.pdf'
instead of directory.root.nativeURL + 'sample.pdf'
Solution 2
I solved it via the following code:
const ROOT_DIRECTORY = 'file:///sdcard//';
const downloadFolderName = 'Download';
this.file.createDir(ROOT_DIRECTORY, downloadFolderName, true)
.then((entries) => {
//then your code
fileTransfer.download(fileLocation,this.ROOT_DIRECTORY+this.downloadFolderName+'/'+ 'sample.pdf').then((entry) => {
}
//ends of your code ^^
})
.catch((error) => {
alert('error' + JSON.stringify(error));
});
Comments
-
Jorge Olaf almost 2 years
I'm trying with
ngCordova
but thecordova.file.documentsDirectory
property isnull
.I have also tried combining the use of
ngCordova
withrequestFileSystem
, but still, the file is not saved in the "Download's".Example of my code:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(directory) { var fileTransfer = new FileTransfer(); fileTransfer.download( encodeURI("http://example.com/sample.pdf"), directory.root.nativeURL + 'sample.pdf', function(entry) { console.log("download complete: " + entry.fullPath); }, function(error) { console.log("download error source " + error.source); console.log("download error target " + error.target); console.log("upload error code" + error.code); }, false ); }, function(err){console.error(err)});
Thanks.
-
Jorge Olaf over 7 yearsThis Question is about download file to "Downloads folder" of Android, not about "cordova.file.documentsDirectory null". Definitely, you answer is too poor and not is constructive.
-
Wade Tyler over 7 yearsYou said "I'm trying with ngCordova but the cordova.file.documentsDirectory property is null"
-
Jorge Olaf over 7 yearsYes, and I says "also tray with "also tried combining the use of
ngCordova
withrequestFileSystem
". This question not is aboutdocumentsDirectory
, is about How I can download file and store it on "Downloads Folder" on Android. -
Jorge Olaf over 7 yearsThis look much better. I'll try it. Thanks! +1
-
Wade Tyler over 7 years@OlafErlandsen No problem. Check cordova.apache.org/docs/en/latest/reference/…
-
Jorge Olaf over 7 yearsThis answer works perfectly. I clarify that it is necessary to add write permissions for the application, otherwise it will not be able to download. Thanks @WadeTyler
-
Wade Tyler over 7 years@OlafErlandsen No problem. Glad to help.
-
Luckylooke about 7 years@OlafErlandsen Can you tell me what permission I need for Download directory? I am getting
/storage/emulated/0/Download/montages.json: open failed: EACCES (Permission denied)
. I have tried READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE and MANAGE_DOCUMENTS permissions. Did not helped :/ -
Jorge Olaf about 7 yearsWell, whats Android version are using you?
-
noor over 6 years@Luckylooke Have you found a solution for this ? I am facing the same issu e
-
Luckylooke over 6 years@noor yes I have succeeded with combination of manifest permissions and cordova-plugin-android-permissions for runtime permissions needed for android 5+