iOS file downloads but can't be found
Opened this issue · 3 comments
Using cordova, I have this all working on Android...and mostly on iOS too. However, after the download on iOS I can't find the file anywhere.
Here is my download function:
$scope.downloadLink = function(fileInfo) {
let storage_location = '' ;
if (ionic.Platform.isIOS()) {
storage_location = cordova.file.documentsDirectory ;
} else if (ionic.Platform.isAndroid()) {
storage_location = 'file:///storage/emulated/0/' ;
}
let downloading = function(event) {
var data = event.data ;
if (data[0] != 100) {
var msg = "Downloading file: " +data[0]+ "%" ;
var color = "red" ;
} else {
var msg = "DOWNLOAD COMPLETE!" ;
var color = "green" ;
}
$scope.startStatus(fileInfo.cfID,color,msg) ;
}
let downloaded = function(event) {
setTimeout(function() {
$scope.endStatus(fileInfo.cfID) ;
event.target.removeEventListener(event.name,downloaded) ;
},5000) ;
}
let initialized = function(event) {
downloader.get(urlFile,null,newFileName) ;
event.target.removeEventListener(event.name,initialized) ;
} ;
document.addEventListener('DOWNLOADER_initialized', initialized) ;
document.addEventListener('DOWNLOADER_downloadProgress',downloading) ;
document.addEventListener('DOWNLOADER_downloadSuccess', downloaded) ;
downloader.init({folder: 'download', fileSystem: storage_location})
}
in xCode, I get the following write line, its like an extra /
is being added to the write path:
-[CDVFileTransfer download:] [Line 423] File Transfer downloading file...
2020-12-09 02:33:06.476605-0500 AttendaGo[6154:7065118] -[CDVFileTransferDelegate connection:didReceiveResponse:] [Line 771] Streaming to file /var/mobile/Containers/Data/Application/EADA1122-72EB-4A9A-8BF9-75D3F57E0147/Documents/download//Word1.DOC
2020-12-09 02:33:06.484266-0500 AttendaGo[6154:7065129] File Transfer Finished with response code 200
2020-12-09 02:33:06.484548-0500 AttendaGo[6154:7065129] -[CDVFileTransferDelegate connectionDidFinishLoading:] [Line 637] File Transfer Download success
Specifically, this is the path, at the end, before the file name is the double //
-
/var/mobile/Containers/Data/Application/EADA1122-72EB-4A9A-8BF9-75D3F57E0147/Documents/download//Word1.DOC
On Android, I can find the file in the download
folder, but on iOS there is no such folder, and even if there was, the double // before the file name would still mess things up.
How can I get this file to save to the root Documents
or Downloads
folder...the public one....on iOS?
=========
update
I then outputted the file systems the plugin is grabbing and got this, Can anyone tell me whats happening here?
document.addEventListener('DOWNLOADER_gotFileSystem', function() {
console.log(event.data) ;
}) ;
document.addEventListener('DOWNLOADER_gotFolder', function() {
console.log(event.data) ;
}) ;
2020-12-09 10:10:00.585683-0500 AttendaGo[6196:7131475] [{"isFile":false,"isDirectory":true,"name":"","fullPath":"/","filesystem":"<FileSystem: persistent>","nativeURL":"file:///var/mobile/Containers/Data/Application/7C66DE16-73C0-4F10-99B8-8D42B8A01920/Documents/"}]
2020-12-09 10:10:00.592567-0500 AttendaGo[6196:7131475] [{"isFile":false,"isDirectory":true,"name":"download","fullPath":"/download/","filesystem":"<FileSystem: persistent>","nativeURL":"file:///var/mobile/Containers/Data/Application/7C66DE16-73C0-4F10-99B8-8D42B8A01920/Documents/download/"}]
2020-12-09 10:10:00.594968-0500 AttendaGo[6196:7131475] -[CDVFileTransfer download:] [Line 423] File Transfer downloading file...
2020-12-09 10:10:01.026073-0500 AttendaGo[6196:7131762] -[CDVFileTransferDelegate connection:didReceiveResponse:] [Line 771] Streaming to file /var/mobile/Containers/Data/Application/7C66DE16-73C0-4F10-99B8-8D42B8A01920/Documents/download//Word1.DOC
2020-12-09 10:10:01.039425-0500 AttendaGo[6196:7131832] File Transfer Finished with response code 200
2020-12-09 10:10:01.040594-0500 AttendaGo[6196:7131832] -[CDVFileTransferDelegate connectionDidFinishLoading:] [Line 637] File Transfer Download success
This issue is only happening on iOS....the double //
at the end of the write-to-path is def causing the issues, but trying to figure out where its coming from has been challenging:
I found these two areas that deal with removing/adding /
from/to the path:
downloader.js, line 136 removes extra /
s from the original filenames, not certain its working though...I think the leading /
before the actual filename is not being removed, so later when a /
is added back to the write-to-path its causing the double //
var fileObject = {
url: url,
name: name || url.replace(/^.*\//, ""),
md5: md5
};
and line 203 adds a /
back to the path before the file name:
/**
* @param {FileObject} fileObject
*/
transferFile: function(fileObject) {
//console.log("tranfserFile");
var filePath = Downloader.localFolder.toURL() + "/" + fileObject.name;
Downloader.transfer = new FileTransfer();
Downloader.transfer.onprogress = function(progressEvent) {
if (progressEvent.lengthComputable) {
var percentage = Math.floor(progressEvent.loaded / progressEvent.total * 100);
document.dispatchEvent(createEvent("DOWNLOADER_downloadProgress", [percentage, fileObject.name]));
}
};
Downloader.transfer.download(fileObject.url, filePath, function(entry) {
// console.log("transferFile, succcess file name: " + Downloader.fileObjectInProgress.name);
document.dispatchEvent(createEvent("DOWNLOADER_downloadSuccess", [entry]));
}, function(error) {
// console.log("transferFile, error file name: " + Downloader.fileObjectInProgress.name);
document.dispatchEvent(createEvent("DOWNLOADER_downloadError", [error]));
});
},
Think I will need to parse the filePath and remove double //
with a regex. Be easier than trying to account for iOS
vs Android
fileSystem structures.
I fixed that issue by adding:
filePath = filePath.replace(/\/\//g, "/") ;
However, now I am getting another error:
Unhandled Promise Rejection: TypeError: null is not an object (evaluating 'result.lengthComputable')
Its coming from line 202 in FileTransfer.js of the cordova-plugin-file-transfer
plugin.