Download file on iOS device with Apache Cordova
In this tutorial we are going to download a image of internet to a iOS device (Iphone 5s), then show the image downloaded in a img tag of html.
Create the application
cordova create MyApp
cd MyApp
Install the Plugin : Cordova Plugin File Transfer
cordova plugin add cordova-plugin-file-transfer
We will use the plugin Cordova Plugin File Transfer, you can read more about this plugin in : Cordova Plugin File Transfer
Add Javascript code
We are going to download the logo of Github, from Here, On index.js file, we need add the next code:
var url = "https://assets-cdn.github.com/images/modules/logos_page/";
var filename = "Octocat.png";
var fileTransfer = new FileTransfer();
var uri = encodeURI(url + filename);
var fileURL = "cdvfile://localhost/persistent/" + filename;
fileTransfer.download(
uri, fileURL, function(entry) {
console.info("Download complete: " + entry.toURL());
document.getElementById("imageDownloaded").src= cordova.file.applicationStorageDirectory + "Documents/"+ filename;
},
function(error) {
console.warn("Download error source " + error.source);
},
false, {
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);
In the index.html file you need add a tag img, Here the downloaded image will be displayed
<img id="imageDownloaded" src=""/>
Ok, this simple code downloads an image and shows it in an img tag on the index.html page.