Downloading a file with Cordova - android

Despite the many examples found on the documentation or in this forum, I can't find a way to download a file using Cordova...
First, I get the rootFS:
function gotFS(fileSystem) {
console.log("got filesystem");
// save the file system for later access
console.log(fileSystem.root.fullPath);
// displays "/" on desktop
// displays "file:///mnt/sdcard" on android with SD Card
window.rootFS = fileSystem.root;
}
document.addEventListener('deviceready', function() {
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, function(){
console.log("error getting LocalFileSystem");
});
}, false);
The upload script:
// Creating the image directory
imageDir = rootFS.getDirectory("imagesContent", {create: true},
function(){
// Success
},
function(error){
console.log("ERROR getDirectory");
console.log(error);
}
);
// Creating and Downloading the image
imgFile = imageDir.getFile(filename, {create: true, exclusive: true},
function (){
var localPath = rootFS.fullPath+'/imagesContent/'+filename;
fileTransfer = new FileTransfer();
fileTransfer.download('http://example.com/images/'+filename,
localPath,
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function (error) {
console.log(error);
console.log('download error: ' + error.code);
console.log("download error source " + error.source);
console.log("download error target " + error.target);
}
);
},
function (error){
console.log("ERROR getFile");
console.log(error);
}
);
I get this error in the console:Uncaught TypeError:
Cannot call method 'getFile' of undefined
The uri is authorized in config.xml.

I think that the problem is with this line: imageDir = rootFS.getDirectory("imagesContent", {create: true},function(), function()) I don't think the call to getDirectory is supposed to actually return the directory name, like what you expect, which is why your imageDir object is undefined.
It might be easier for you to use the FileTransfer.download() method to download stuff from your server: http://docs.phonegap.com/en/edge/cordova_file_file.md.html#FileTransfer
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");
fileTransfer.download(
uri,
filePath,
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);

var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");
fileTransfer.download(
uri,
filePath,
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);
I use it but success and download is completed but not found image in y app

Related

Phonegap Download PDF from URL using File Transfer Plugin

I have a problem with downloading a pdf from an external url and save it on the android device. So far I get these errors:
download error source http://bbg.co.tz/wp-content/uploads/2015/03/CC008962_Tanzania-tax-briefing-transfer-pricing_18-11-15.pdf
index.js:227 download error target /Phone/Download
index.js:228 upload error code1
And here is my code:
function bbgDownload(bbgURL)
{
var fileTransfer = new FileTransfer();
var uri = encodeURI(bbgURL);
fileURL='/Phone/Download'
fileTransfer.download(
uri,
fileURL,
function(entry) {
console.log("download complete: " + entry.toURL());
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);
console.log("Please show this URL " + bbgURL);
}
Append a filename to your fileURL variable. This worked for me, for example:
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://bbg.co.tz/wp-content/uploads/2015/03/CC008962_Tanzania-tax-briefing-transfer-pricing_18-11-15.pdf");
var fileURL = "cdvfile://localhost/persistent/mydir/myPdfFile.pdf";
fileTransfer.download(
uri,
fileURL,
function(entry) {
console.log("download complete: " + entry.toURL());
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);

Cannot save to file on Android and Apache Cordova 3.4.1

Cordova version: 3.4.1-0.1.0
I am using the file transfer plugin and I cannot save a image to file.
Errors I get from the device:
"download error target err"
upload error code 1
Here is the code, any ideas?
var fileTransfer = new FileTransfer(),
d = new Date(),
testImageUrl = encodeURI('http://www.inutritie.ro/wp-content/uploads/2013/07/otet-600x300.jpg'),
imageCacheDir = '//Testdir';
fileTransfer.download(
testImageUrl,
'file:'+imageCacheDir+'/'+d.getTime(),
function(entry){
alert("download complete: " + entry.fullPath);
},
function(error){
alert("download error source " + error.source);
alert("download error target " + error.target);
alert("upload error code" + error.code);
}
);
Here is how I set and get the directory where I'll save the images on my Android device:
function initLocalCacheDir(){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onFileSystemFail);
};
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getDirectory("Testdir",{create:true, exclusive: false}, gotDir, gotDirError);
};
function gotDir(d) {
var imageCacheDir = d.fullPath;
};
function gotDirError(evt) {
alert('gotDirError');
};
function onFileSystemFail(evt) {
alert('onFileSystemFail');
};

error to filetransfer.download in android (erro code 1)

document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
//request the persistent file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);
}
function fileSystemSuccess(fileSystem) {
var directoryEntry = fileSystem.root; // to get root path to directory
directoryEntry.getDirectory("teste_recev", {create: true, exclusive: false}, onDirectorySuccess, onDirectoryFail);
var rootdir = fileSystem.root;
var fp = rootdir.fullPath;
fp = fp+"/teste_recev/image_name.jpg";
var fileTransfer = new FileTransfer();
fileTransfer.download("https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/t1.0-1/c14.4.48.48/150734_264955536974736_682293823_t.jpg",fp,
function(entry) {
alert("download complete: " + entry.fullPath);
},
function(error) {
alert("download error source " + error.source);
alert("download error target " + error.target);
alert("upload error code" + error.code);
}
);
}
function onDirectorySuccess(parent) {
console.log(parent);
}
function onDirectoryFail(error) {
alert("Unable to create new directory: " + error.code);
}
function fileSystemFail(evt) {
console.log(evt.target.error.code);
}
But have a error:
dowload error source https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/t1.0-1/c14.4.48.48/150734_264955536974736_682293823_t.jpg
download error target//teste_recev/image_name.jpg
upload error code1
i have, the cordova.js in project, have link for cordova.js but have this erros if someone to help, really appreciate it.
Try like this
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function gotFS(fileSystem) {
fileSystem.root.getDirectory("your dir", {create: true}, function fileSystemSuccess(fileSystem){
fileSystem.getFile("dummy.txt",{create: true,exclusive:false},function gotFileEntry(fileEntry){
var path = fileEntry.fullPath.replace("dummy.txt","");
fileEntry.remove();
var fileTransfer = new FileTransfer();
fileTransfer.download(FILE_DOWNLOAD_URL, path+""+your -savedName,function(theFile){
alert("File Downloaded Successfully " + theFile.toURI());
},function(error){
alert("File Transfer failed" + error.message);
});
},fail);
});
}, fail);

forcing phonegap to use internal storage with fileTransfer download method

I am developing a phonegap applicaiton that downloads a file from the remote location like this:
var remoteFile=url+item.image;
var localFileName=item.image;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
"dummy.html", {create: true, exclusive: false},
function gotFileEntry(fileEntry) {
var sPath = fileSystem.fullPath.replace("dummy.html","");
var fileTransfer = new FileTransfer();
fileEntry.remove();
fileTransfer.download(
remoteFile,
sPath + localFileName,
function(theFile) {
alert("download complete: " + theFile.toURI());
showLink(theFile.toURI());
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code: " + error.code);
}
);
}, fail);
}, fail);
function fail(error) {
alert(error.code);
}
This downloads the files to a SD Card. To prevent external users from downloading the files, i want the app to forcefully store the files in device internal storage. What are my options?
Further, i need to retrieve the file (image) and display the same at some other point of time!

Downloading pdf file in SD card using Phone Gap in android

I want to download pdf file in my android phone using phone gap.can any one help me to download it? i have used https://github.com/phonegap/phonegap-plugins/tree/master/Android/Downloader plugin but it is not working.If anybody have any other downloading method then please let me know..
Thanks in advance..
You can use the phonegap file api to download the files you want from server. Check docs for more detail - FileTransfer API
Here is an example code to download and save a pdf file in your sdcard.
window.appRootDirName = "download_test";
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log("device is ready");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function fail() {
console.log("failed to get filesystem");
}
function gotFS(fileSystem) {
console.log("filesystem got");
window.fileSystem = fileSystem;
fileSystem.root.getDirectory(window.appRootDirName, {
create : true,
exclusive : false
}, dirReady, fail);
}
function dirReady(entry) {
window.appRootDir = entry;
console.log("application dir is ready");
}
downloadFile = function(){
var fileTransfer = new FileTransfer();
var url = "http://www.irs.gov/pub/irs-pdf/fw4.pdf";
var filePath = window.appRootDir.fullPath + "/test.pdf";
fileTransfer.download(
url,
filePath,
function(entry) {
alert("download complete: " + entry.fullPath);
},
function(error) {
alert("download error" + error.source);
}
);
}
For full source - https://gist.github.com/3055240
** You must add plugin file API, file transfer in phonegap (3.0+)
function downloadPDF(url, fileName, folder) {
var fileTransfer = new FileTransfer();
var uri = encodeURI(url);
var fileURL = folder + fileName;
fileTransfer.download(
uri,
fileURL,
function(entry) {
alert("complete");
console.log("download complete: " + entry.toURL());
},
function(error) {
alert("upload error code" + error.code);
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);
}
in Link
<a href="#"
onclick="
downloadPDF(
'http://cran.r-project.org/doc/manuals/R-intro.pdf',
'R-intro.pdf',
'/sdcard/nuuoeiz/'
);">
Download PDF</a>

Categories

Resources