我是PhoneGap编程新手。我的问题是
如何在iOS应用程序内部存储下载的PDF文件。以下是我的文件:
function downloadFile() {
alert('start');
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
window.resolveLocalFileSystemURI("http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf", onResolveSuccess, fail);
}
function onFileSystemSuccess(fileSystem) {
alert(fileSystem.name);
console.log(fileSystem.name);
}
function onResolveSuccess(fileEntry) {
alert('success');
console.log(fileEntry.name);
}
function fail(evt) {
console.log(evt.target.error.code);
}
我还添加了插件asCordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer.git
但它不起作用。我们需要这个LocalSystem的任何插件吗?
这是下载文件到ios文档目录的完整代码
document.addEventListener('deviceready',onDeviceReady, false);
function onDeviceReady()
{
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,onFileSystemSuccess, fail);
}
function fail() {
console.log("failed to get filesystem");
}
function onFileSystemSuccess(fileSystem) {
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf");
fileTransfer.download(
uri,
fileSystem.root.fullPath+"/MyPdf.pdf",
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);
}
);
}
这将把Nitobi.pdf文件下载到documents目录中为MyPdf.pdf
供参考。
* * * * * * *编辑 *********
对于phonegap 3.4版本,需要将filessystem .root. fullpath +"/MyPdf.pdf"替换为cdvfile://localhost/persistent/MyPdf.pdf
由Phonegap 3.4 FileTransfer错误(iOS)提供