【发布时间】:2012-04-06 09:37:43
【问题描述】:
我正在编写一个需要下载文件(pdf、doc、txt)的 phonegap 应用程序。
我正在使用 phonegap 1.5.0,即 cordova 1.5.0.js 文件。
我查看了 phonegap 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: 文件类型,即 pdf 或 doc 或 png。
url: 实际资源的 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 不支持下载方式。虽然日志已经说它能够创建我想要的所有目录。
【问题讨论】:
标签: windows-phone-7 blackberry jquery-mobile cordova