I am writing a phonegap application which needs to download a file (pdf,doc,txt).
I am using phonegap 1.5.0 i.e. cordova 1.5.0.js file.
I looked into the phonegap api at
http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html#FileTransfer
and trying to use FileTransfer’s download method. following is the code which I am using:
save: function (fileName, fileType, url) {
documentsaver.fileName = fileName;
documentsaver.fileType = fileType;
documentsaver.url = url;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fail);
function fail(event) {
jqmSimpleMessage('Error Code ' + event.target.error.code);
}
function fsSuccess(fileSystem) {
documentsaver.directoryEntry = fileSystem.root;
//Creating directory in which document should be saved if it does not exist
documentsaver.directoryEntry.getDirectory(documentsaver.directoryName, { create: true, exclusive: false }, dirSuccess, fail);
function dirSuccess(parent) {
console.log('Directory Created at '+parent.fullPath+' with name '+parent.name);
//Moving directoryEntry reference to newly created directory
documentsaver.directoryEntry = parent;
//Creating file which will be written
var completeFileName = documentsaver.fileName + '.' + documentsaver.fileType;
console.log('completeFileName === >' + completeFileName );
var filePath = documentsaver.directoryEntry.fullPath + '/' + completeFileName;
console.log('filePath === >' + filePath );
var fileTransfer = new FileTransfer();
fileTransfer.download(
url,
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);
}
);
}
fileName: name of the file which I am saving.
fileType: fileType i.e. pdf or doc or png.
url: url to the actual resource.
Following is the console log when i run it on windows emulator:
Log:”This is a dir”
The thread ” (0xf0a01c6) has exited with code 0 (0x0).
Log:”filePath === >/JarusDocuments/Personal Auto Application.pdf”
The thread ” (0xff001f6) has exited with code 0 (0x0).
Log:”Directory Created at /JarusDocuments with name JarusDocuments”
Log:”Error in success callback: File11 = Object doesn’t support property or method ‘download'”
The thread ” (0xe3201b6) has exited with code 0 (0x0).
The thread ” (0xf18014e) has exited with code 0 (0x0).
Log:”completeFileName === >Personal Auto Application.pdf”
The thread ” (0xf1c01de) has exited with code 0 (0x0).
It is saying that FileTransfer does not support download method. Although log already says that it is able to create all the directories that I want.
In Phonegap 1.5 for WP7 the FileTransfer object is not download capable (only upload). The 1.6 version however claims to be capable of doing just that (you can read it for yourself in phonegap’s blog post about the release here)