apache / cordova-plugin-file-transfer

Apache Cordova File Transfer Plugin
https://cordova.apache.org/
Apache License 2.0
598 stars 885 forks source link

`fileTransfer.download` is not working in iOS 11.4.1 #201

Closed rachithhegde closed 4 years ago

rachithhegde commented 6 years ago

On clicking of download ,it is suddenly showing download complete,but the downloaded file is corrupted.

janpio commented 6 years ago

What exactly are you talking about? What version of Cordova CLI, Cordova iOS, the plugin are you using? What is the code that is being executed?

rachithhegde commented 6 years ago

This is my code for download video file from the server for IOS version(11.4.1)

fileTransfer.download(url, this.file.dataDirectory + 'file.mp4').then((entry) => {
    console.log('download complete: ' + entry.toURL());
  }, (error) => {
    // handle error
  });

ionic version: 3.19.1 node version: 8.11.3 cordova version: 8.0.0 cordova-plugin-file-transfer version: 1.7.1 node module @ionic-native/file-transfer version: 4.11.0 platform Ios version: 4.5.5

janpio commented 6 years ago

Does it work in other iOS versions? How are you learning that the file is corrupted?

rachithhegde commented 6 years ago

In ios 10.3.0 is also it is not working. I am fetching the file list from the dataDirectory and trying to play it is not playing. File size is around 30mb, In fileTransfer.onProgress event we are getting 0 and then directly 100 and file downloaded success is showing in alert.

richardshergold commented 6 years ago

I'm seeing the same thing today in my Ionc app running on iOS 11.4.1 (and it was working fine in earlier versions of iOS)

I'm doing this:

fileTransfer.download(file, this.file.dataDirectory + 'file.' + ext)

and I see this in the console:

File Transfer Finished with response code 200

and then..

ERROR: {"__zone_symbol__currentTask":{"type":"microTask","state":"notScheduled","source":"Promise.then","zone":"angular","cancelFn":null,"runCount":0},"line":1,"column":4972,"sourceURL":"file:///var/containers/Bundle/Application/4F128730-988B-41F2-8C34-407C1DEEEC9D/myApp.app/www/build/18.js"}

Ionic Info:

Ionic:

   ionic (Ionic CLI)  : 4.0.6 (/usr/local/lib/node_modules/ionic)
   Ionic Framework    : ionic-angular 3.9.2
   @ionic/app-scripts : 3.1.11

Cordova:

   cordova (Cordova CLI) : 8.0.0
   Cordova Platforms     : android 7.1.0, ios 4.5.5

System:

   ios-deploy : 2.0.0
   NodeJS     : v8.11.3 (/usr/local/bin/node)
   npm        : 6.3.0
   OS         : macOS High Sierra
   Xcode      : Xcode 9.4.1 Build version 9F2000

Environment:

   ANDROID_HOME : not set
janpio commented 6 years ago

Does this happen all the time or just sometimes @richardshergold @rachithhegde? You can't confirm that it also happens on iOS 10.3.0 @richardshergold (as @rachithhegde indicated in https://github.com/apache/cordova-plugin-file-transfer/issues/201#issuecomment-412838124)?

richardshergold commented 6 years ago

@janpio sorry for the delay - I'm in the UK. it happens all the time on any type of attachment I try. I will get one of my testers to try an iOS 10 device and let you know. Apart from the change to iOS 11.4 on my device, one additional change in my own project (since this was all working) has been raising Cordova iOS from 4.5.0 to 4.5.5 - do you know if that might be connected to this issue?

janpio commented 6 years ago

(No worries about any wait time - just trying to collect all information)

raising Cordova iOS from 4.5.0 to 4.5.5 - do you know if that might be connected to this issue?

No idea, should not affect it - but it would be glorious if you could check it out and report back. Downgrading should not be too hard I hope.

richardshergold commented 6 years ago

@janpio apparently it is also affecting older versions of iOS too (my tester has just reported issues on a 10.3.2 device I think). I will see if I can look into the Cordova IOS thing later.

richardshergold commented 6 years ago

@janpio This its also not working on Android 7 so I'm starting to think this may not be an issue with your plugin at all as that seems a bit weird as it was all working before. Possibly something to do with the file plugin or the version of the @ionic-native/file wrapper. I will try and do some tests myself today and let you know if I find anything.

richardshergold commented 6 years ago

@janpio ignore that last comment. Android is working fine. I will look at the IOS issue now....

richardshergold commented 6 years ago

@janpio ok now it seems to be working for me in iOS too (iOS 11 anyway) so I'm not sure what was going on before. It certainly all seems ok now anyway :-)

janpio commented 6 years ago

So now everything ok for you again @richardshergold?

richardshergold commented 6 years ago

At the moment yes, I will report back if I have any issue.

rachithhegde commented 6 years ago

for me still same issues in ios 11.4.1 @richardshergold please tell us what you did which resolves the issues.

richardshergold commented 6 years ago

@rachithhegde I haven't changed anything - I think I was mistaken into seeing the issue in the first place.

rachithhegde commented 6 years ago

can you please tell us the file transfer plugin version and file transfer node module version?

$ ionic cordova plugin add cordova-plugin-file-transfer--- version? $ npm install --save @ionic-native/file-transfer-- version?

richardshergold commented 6 years ago

cordova-plugin-file-transfer 1.7.1 "File Transfer" "@ionic-native/file-transfer": "^4.11.0"

Full Ionic Info:

Ionic:

ionic (Ionic CLI) : 4.0.6 (/usr/local/lib/node_modules/ionic) Ionic Framework : ionic-angular 3.9.2 @ionic/app-scripts : 3.1.11

Cordova:

cordova (Cordova CLI) : 8.0.0 Cordova Platforms : android 7.1.0, ios 4.5.5

System:

ios-deploy : 2.0.0 NodeJS : v8.11.3 (/usr/local/bin/node) npm : 6.3.0 OS : macOS High Sierra Xcode : Xcode 9.4.1 Build version 9F2000

Environment:

ANDROID_HOME : not set

rachithhegde commented 6 years ago

We have tried with the above specified platform version still the problem is not resolved.

cap

we are look for the solution like the attached image.

BrainstemStudio commented 6 years ago

Can confirm that downloads are not working on big sizes (15mb and above) in iOS 11.4. This was working in older versions of iOS

BrainstemStudio commented 6 years ago

Workaround. You can place headers in the download process and use "if-Modified-Since" and set it some date in the past. I think the problem is that on iOS 11.4 file downloads appear to be called from cache causing a download success with a partial file.

This worked for us.

ninexus commented 5 years ago

we have also same problem, trying to download the pdf file , it get downloaded 0 byte making it corrupt. , @BrainstemStudio how the header to be added can you suggest some example will be helpful

BrainstemStudio commented 5 years ago

@ninexus read the docs here https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file-transfer/#download

Example:

/**
 * Download a file example
 * Accepts the parameters: source, target, successCallback, errorCallback, trustAllHosts and options.
 * Options can contain headers as shown below.
 * 
 * source          : The file to download, make sure to "encodeURI"
 * target          : where to save the file locally, use cordova.file for root. See https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/
 * successCallback : function to run once the file has downloaded. Passes entry.
 * errorCallback   : function to run if there is an error. Passes error
 * trustAllHosts   : Must be false for production.
 * options         : Support for headers. Authorization or cache control.
 */
var fileTransfer = new FileTransfer();

fileTransfer.download(
    encodeURI('http://some.server.com/picture.png'),
    cordova.file.dataDirectory+'your-folder/picture.png',
    function(entry) {},
    function(error) {},
    false,
    {
        headers: {
            "if-Modified-Since" : "Wed, 21 Oct 2015 07:28:00 GMT"
        }
    }
);
BrainstemStudio commented 5 years ago

@ninexus Also read https://cordova.apache.org/blog/2017/10/18/from-filetransfer-to-xhr2.html as you will notice cordova has deprecated this plugin in favor of standards.

But the if-Modified-Since fix should resolve the issue as it did for us.

ninexus commented 5 years ago

@BrainstemStudio thanks for the suggestion we tried the given suggested code its still download 0 byte pdf file, please take a look if there is anything you can recommend in it.

ft.download(encodeURI(WC_API.pdf_file_server +'/'+ parshaid), 'cdvfile://localhost/persistent/.systems/caches/data/file/file'+parsha_id+'.pdf',function(entry) { console.log("download complete: " + entry.toURL()); $scope.pdfurl = entry.toURL(); $ionicLoading.hide(); PDFFileService.updateData({ is_downloaded: 1, source: entry.toURL(), parsha_id: parsha_id }); console.log($scope.pdfurl); openPdfDoc($scope.pdfurl, $scope.parsha_name); }, function(error) { console.log(error); $ionicLoading.hide(); alert('Failed opening pdf, server error.'); }, false, { headers: { "if-Modified-Since" : "Wed, 21 Oct 2015 07:28:00 GMT" } });

BrainstemStudio commented 5 years ago

@ninexus I would suggest checking console errors and debug any network issues. Also you may want to use "onprogress" method for downloads and check whats happening as it downloads.

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file-transfer/#properties

e.g

ft.onprogress = function(e) {
    if (e.lengthComputable){
        var percent = (e.loaded / e.total) * 100;
        console.log(percent+'% downloaded');
        console.log(e);
    }
    else{
        console.log(e);
    }
}
santhoshbabu-d commented 5 years ago

Download File is corrupted in the Android Kitkat 4.4, installed plugin today

import { FileTransfer ,FileTransferObject} from '@ionic-native/file-transfer/ngx'; import { File } from '@ionic-native/File/ngx';

constructor( private transfer: FileTransfer, private file: File,...)

download(){ let downloadUrl = encodeURI('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf'); let path = 'cdvfile://localhost/persistent/Download/'; const transfer = this.transfer.create(); transfer.download(downloadUrl, path + 'dummy.pdf').then(entry => { let url = entry.toURL(); console.log('File url\n' + url); }, (error) => { // handle error console.log("download error source " + error.source); console.log("download error target " + error.target); console.log("download error code" + error.code); }); }

BrainstemStudio commented 5 years ago

@santhoshbabu-d For some reason android 4.4 below doesn't play well with https connections when downloading files. We ran into a similar issue and had to use http when android 4.4 was detected.

santhoshbabu-d commented 5 years ago

its downloading but file corrupted

santhoshbabu-d commented 5 years ago

from what version this plugin support

BrainstemStudio commented 5 years ago

@santhoshbabu-d try the "if-Modified-Since" header fix described above

santhoshbabu-d commented 5 years ago

tried the download from http showing same issue,file corrupted,

Also showing erro when adding header in fileTransfer (Ionic 4.12.0) error TS2554: Expected 0-2 arguments, but got 4.

BrainstemStudio commented 5 years ago

@santhoshbabu-d I don't think the plugin is at fault and this is likely to do with the connection to the file you are trying to download. When inspecting the headers of the file you are trying to download i see a few modern headers such as "strict-transport-security" which may causes issues in lower versions of android devices. My suggestion is to try the following:

  1. Test with a file on your own server or from a localhost environment.
  2. Use a android version detection and downgrade the connection to http for < 4.4 (when downloading files). You can do this with "navigator.userAgent".
  3. Use https://www.ssllabs.com/ssltest/ to test server settings. There has been lots of changes to secure connections which will cause problems on old devices more specifically ones that only support TSL 1.0
  4. Test with a modern device or os version.

FYI Cordova is unlikely to update this plugin in favor of standards. Source: https://cordova.apache.org/blog/2017/10/18/from-filetransfer-to-xhr2.html.

santhoshbabu-d commented 5 years ago

Checked with the Android 5.0.2 issue looks same

santhoshbabu-d commented 5 years ago

Thanks for the help, finally it work after update ionic

timbru31 commented 4 years ago

I'm closing this issue since it's stale and sounded like a potential bug in iOS 11.4.1 with some workarounds.

If the issue persists, please create a new one, follow the template and most importantly create a minimum reproducible example.