我正在写一个需要下载文件的电话空白应用程序(pdf,doc,txt) 我正在使用phonegap 1.5.0,即cordova 1.5.0.js文件。
我看了一下的电话号码api
http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html#FileTransfer
并尝试使用FileTransfer的下载方法。以下是我正在使用的代码:
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:我正在保存的文件的名称
fileType: fileType,即pdf或doc或png
url:指向实际资源的网址
以下是我在Windows模拟器上运行时的控制台日志:
记录:“这是一个目录”
线程''(0xf0a01c6)已退出,代码为0(0x0)
记录:“filePath ===> / JarusDocuments / Personal Auto Application.pdf”
线程''(0xff001f6)已退出,代码为0(0x0)
日志:“在名为JarusDocuments的/ JarusDocuments中创建的目录”
日志:“成功回调错误:File11 =对象不支持属性或方法'下载'”
线程''(0xe3201b6)已退出,代码为0(0x0)
线程''(0xf18014e)已退出,代码为0(0x0)
记录:“completeFileName ===> Personal Auto Application.pdf”
线程''(0xf1c01de)已退出,代码为0(0x0)。
据说FileTransfer不支持下载方法。虽然log已经说它能够创建我想要的所有目录。
答案 0 :(得分:1)
在WP7的Phonegap 1.5中,FileTransfer对象无法下载(仅上传)。然而,1.6版本声称能够做到这一点(你可以在phonegap关于发布的博客帖子中自己阅读here)