I've read a lot of posts but I got no the FINAL answer. Starting from the code at this link, I got my file downloaded into the app. Anyway, I'd like to see it into the "Download" folder. I'm using Android, but clearly I'd like a solution valid for iOS also.
Asked
Active
Viewed 3.5k times
1 Answers
15
EDIT
If you already know the path of the file you can just move it:
var storageLocation = "";
console.log(device.platform);
switch (device.platform) {
case "Android":
storageLocation = 'file:///storage/emulated/0/';
break;
case "iOS":
storageLocation = cordova.file.documentsDirectory;
break;
}
var fileUri = "file:///data/user/0/com.arsdigitalia.myapp/files/files/MyApp/test.pdf"
function moveFile(fileUri) {
window.resolveLocalFileSystemURL(
fileUri,
function(fileEntry){
var parentEntry = storageLocation + "Download";
// move the file to a new directory and rename it
fileEntry.moveTo(parentEntry, "newFile.pdf", success, fail);
},
errorCallback);
}
Original
Here is a sample piece of code I use to accomplish this. It works best on Android, iOS is a bit different due to the app sandboxing so you need to handle retrieving files yourself. I also use the Cordova device plugin to determine what device the app is running on, I can then change storage paths to suit:
var storageLocation = "";
console.log(device.platform);
switch (device.platform) {
case "Android":
storageLocation = 'file:///storage/emulated/0/';
break;
case "iOS":
storageLocation = cordova.file.documentsDirectory;
break;
}
window.resolveLocalFileSystemURL(storageLocation,
function (fileSystem) {
fileSystem.getDirectory('Download', {
create: true,
exclusive: false
},
function (directory) {
//You need to put the name you would like to use for the file here.
directory.getFile("YOUR_FILE_NAME", {
create: true,
exclusive: false
},
function (fileEntry) {
fileEntry.createWriter(function (writer) {
writer.onwriteend = function () {
console.log("File written to downloads")
};
writer.seek(0);
writer.write(YOUR_FILE_HERE); //You need to put the file, blob or base64 representation here.
}, errorCallback);
}, errorCallback);
}, errorCallback);
}, errorCallback);
var errorCallback = function(e) {
console.log("Error: " + e)
}
Then to retrieve the file list from the directory you can use:
window.resolveLocalFileSystemURL(storageLocation,
function (fileSystem) {
fileSystem.getDirectory('Download', {
create: true,
exclusive: false
},
function (directory) {
var reader = directory.createReader();
reader.readEntries(function (files) {
if (files.length == 0) {
console.log("No files found in downloads folder.")
} else {
$.each(files, function (i, v) {
console.log("File Name: " + files[i].name;)
});
}
}, getFilesFail);
}, getFilesFail);
}, getFilesFail);
var getFilesFail = function(e) {
console.log("Error: " + e);
}
To install the device plugin use this command:
cordova plugin add cordova-plugin-device
Documentation here:
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-device/

L Balsdon
- 995
- 8
- 12
-
for the line writer.write(YOUR_FILE_HERE); I think I can't use the filepath as YOUR_FILE_HERE. In fact I have: file:///data/user/0/com.arsdigitalia.myapp/files/files/MyApp/test.pdf but the original file of 400kB is not well copied (I found a file of 80B in the downloads folder). Suggestions? – Zappescu Apr 24 '17 at 09:59
-
Ok I used the readBinaryFile explained in the official plugin doc (https://github.com/apache/cordova-plugin-file-transfer#download-a-binary-file-to-the-application-cache-) and I got the file. Just a final answer: I got access to the file using a file manager and seeing into the download folder, anyway, in the "official" download folder in Android 7.1 (the icon on the desk) I can't see it. Why? – Zappescu Apr 24 '17 at 10:24
-
I edited my post to include a function that should move a file from one directory to another. I think the reason you can't see it in the official download app is because it wasn't downloaded to that folder by the Android download manager. – L Balsdon Apr 24 '17 at 10:33
-
Ok, but you should change your function to this one: window.resolveLocalFileSystemURL( fileUri, function (fileEntry) { var parentEntry = storageLocation + "Download"; window.resolveLocalFileSystemURL(parentEntry, function (store) { // move the file to a new directory and rename it fileEntry.moveTo(store, "newFile.pdf", function () { console.log("done moving"); }, function () { console.log("error moving"); }); },errorCallback); }, errorCallback); – Zappescu Apr 24 '17 at 13:15
-
2Are you saying Downloads folder is in `file:///storage/emulated/0/Download` for Android – João Pimentel Ferreira May 20 '18 at 08:46
-
1Does this still work? I think for iOS you are moving a file to a `Download` folder inside the app directory path - which means the user still can't see it in their iPhones general `Downloads` folder. Why has apple made this so hard. You can download files to the users phone, but the user can't see them. – rolinger Mar 06 '23 at 15:00