Setu Kumar Basak
Setu Kumar Basak

Reputation: 12022

How to download a file in iOS (Ionic 2/3)?

I am trying to download a file and save that file in iOS. I am using ionic-native-file plugin to achieve this. The file has been download but i could not find that file in the device.

filewrite = (): void => {
    let transfer = this.fileTransfer.create();
    let path = "";
    let dir_name = 'Download';
    let file_name = "Sample.pdf";

    if (this.platform.is('ios')) {
      this.platform.ready().then(() => {
          path = this.file.documentsDirectory;

          this.file.writeFile(path,file_name,this.pdfSrc).then((entry) => {
            this.showAlert("download completed");
          }, (error) => {
            this.showAlert("Download Failed.");
          }).catch(err=>{
            this.showAlert("Download Failed catch.");
            this.showAlert(err.message);

          });
        }
      )
    }
  };

Download completed alert shows and the downloaded path is:

file:///var/mobile/Containers/Data/Application/6DE22F30-5806-4393-830A-14C8A1F320BE/Library/Cloud/Sample.pdf

But i could not find that location in the device. Then, i google and saw here.

cordova.file.documentsDirectory - Files private to the app, but that are meaningful to other application (e.g. Office files). Note that for OSX this is the user's ~/Documents directory. (iOS, OSX)

So, i can't see the file actually as it is private to the application. Then i saw in the same link:

enter image description here

So, i tried with the both preference in my config.xml but nothing happened.

Is there any approach to download the file iOS or to dropbox or anywhere else?

Upvotes: 5

Views: 3382

Answers (1)

BRass
BRass

Reputation: 3838

I've struggled to find the right directory to save to on different platforms with this cordova plugin too. I landed on using file.externalRootDirectory on Android, and file.cacheDirectory on iOS.

The right location likely depends on what you intend to do with the file. In my case I just needed it stored short-term so I can open it using the user's native PDF reader.

Upvotes: 2

Related Questions