我们有一个phonegap应用程序,可以将一些文件下载到本地文件系统,以使它们脱机使用。这适用于Windows Phone 8和Android。但是,在iOS上运行应用程序时,它不会下载文件并导致应用程序挂起。
这是我们下载文件的代码:
try {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
fs.root.getDirectory(settings.filesRootDir, {
create: true,
exclusive: false
}, function (directory) {
var filePath = directory.toURL() + "/" + attachment.titleSanitized; // full file path
console.log("filePath: " + filePath);
var fileTransfer = new FileTransfer();
fileTransfer.download(
url,
filePath,
function (entry) {
console.log("download complete entry to url: " + entry.toURL());
attachment.fileLocation = entry.toURL();
var attachments = ko.observableArray(store.get("attachments"), []);
attachments.push(attachment);
store.set("attachments", attachments());
defer.resolve();
},
function (error) {
console.log("download error code: " + error.code);
console.log("download error source: " + error.source);
console.log("download error target: " + error.target);
console.log("download error http_status: " + error.http_status);
defer.reject();
},
false,
{
headers: headers
});
})
}, function (error) {
console.log("requestFileSystem error code: " + error.code);
console.log("requestFileSystem error source: " + error.source);
console.log("requestFileSystem error target: " + error.target);
});
}
catch (error) {
alert(error.message);
}
console.log(文件路径)显示:
cdvfile://localhost/persistent/ourapp//Aanvang.pdf
在iPHone配置实用程序的控制台中显示:
<Warning>: File Transfer Finished with response code 200
<Warning>: void SendDelegateMessage(NSInvocation*): delegate (webView:resource:didFinishLoadingFromDataSource:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
然后它只是等待并每10秒输出一次消息
我有一种感觉,我们正在拯救错误的道路,但我无法找到任何关于保存它的文档或提示。请注意,此代码适用于Android / WP8。
使用: Phonegap 3.4.0 org.apache.cordova.file 1.0.1 org.apache.cordova.file-transfer 0.4.2