Reputation: 607
I want to download files from the server and store it on the installed drive applications like iCloud, Google Drive etc on my iPhone. I'm using Ionic 3 and Cordova 8.I'm using cordova-file-transfer plugin for downloading. Below is the target path which I have given for downloading the files.
cordova.file.syncedDataDirectory + filename;
But the file is getting downloaded into app's sandbox storage. User can access it only via iTunes data sharing(By connecting iPhone to mac machine).But I want to save the files into drive applications(how popular apps like whatsapp, facebook, skype does) so that user can view it without connecting device to mac machine. Below is my complete code.
var targetPath = cordova.file.syncedDataDirectory;
const fileTransfer: FileTransferObject = this.transfer.create();
this.base.showLoadingPopUp();
fileTransfer.download(URL,targetPath+attachment,true)
.then((entry) => {
this.message= 'Download Completed' + entry.toURL();
}).catch(e => {
console.log('Download Fail: ' + e);
})
Any help would be appreciated, thanks in advance.
Upvotes: 2
Views: 1762
Reputation: 607
I found answer to this question. cordova-plugin-file can’t save icloud or any other drive applications. Best way to achieve this is using cordova-plugin-x-socialsharing. Install this plugin and import it on to app.module.ts and add an entry to provider. If you are not aware how to install the plugin please follow this link. Below is the simple 2 step algorithm to download file on drive apps.
let dir = this.file.tempDirectory; let fileName = ""; // please set your fileName; let blob = ""; // please set your data;
try {
this.file
.writeFile(dir,
fileName,
blob,
{replace: true})
.then(value => {
this.socialSharing.share(null, null, null, value.nativeURL);
});
} catch (e) {
console.log(e);
}
Above should work fine. If after downloading the file, if you face any problem in viewing the file, then just user social sharing as shown below.
try {
this.socialSharing.share(null, null,file_native_url, null).then(e =>{
console.log("SUCCESS IN SHARING",e)
}).catch(e =>{
console.log("FAILURE IN SHARING",e)
})
} catch (e) {
console.log("ERROR IN SAVING TO CLOUD",e);
}
Upvotes: 1