Reputation: 67
I have encounter this issue recently while using Ionic3 to develop app on Android phone. I have cols and rows of data which I extracted into a 2D array. After which, I create a csv blob from the 2D array using papaparse.
let csv = papa.unparse({
fields: this.headerRow,
data: this.csvData
});
var blob = new Blob([csv]);
var a = window.document.createElement("a");
a.href = window.URL.createObjectURL(blob);
a.download = "confidentialData.csv";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
The code above works only for browser download, when I load it into my android phone, it does not download at all. I have read many other posts on how Ionic app download file into the device using FileTransfer and File. But I cant seem to work around with these because FileTransfer.download requires a url source in order to download the file into the device.
But for my case, the file is not hosted anywhere and the blob is generated dynamically from my code. Would appreciate any help or recommendation on how I can download my csv blob into my android device using Ionic 3. Thank you very much.
Upvotes: 1
Views: 3419
Reputation: 397
You are getting it wrong. FileTransfer works as an upload or download from an url (For example uploading a picture to a server from your device or downloading a picture from the server to your device).
What you want to look at is Cordova File and use the functions createFile
and writeFile
to save the data on the device.
EDIT: To save your file on the Documents or Downloads folder you will need to use cordova.file.externalRootDirectory + '/Download/'
for it to work, also take in mind that a lot of folders are just read only (dataDirectory on iOS for example)
Hope this works.
Upvotes: 5