在安卓上我用过
file:///storage/sdcard0/Android/data/my.app.id/cache/
下载(FileTransfer)一些图像,然后在我的 html 中显示它们。删除我的应用程序时,此文件夹中的图像和其他所有内容都会被删除,这正是我想要的。
如何在 iOS 上实现这一点?
我试过
file:///var/mobile/Applications/<GUID of app>/Documents/
因为这是我在请求文件系统时得到的,但图像甚至不会下载(错误代码 1)。有任何想法吗?我发现了更多关于我的错误的信息:它与 this 中所述的错误相同问题。 (无法创建保存下载文件的路径 - Cocoa 错误 512)
谢谢
更多信息: 我使用的相关插件是
<gap:plugin name="org.apache.cordova.file" />
<gap:plugin name="org.apache.cordova.file-transfer" />
和特点:
<feature name="File">
<param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="File">
<param name="ios-package" value="CDVFile" />
</feature>
<feature name="FileTransfer">
<param name="ios-package" value="CDVFileTransfer" />
</feature>
我在 Android 中成功下载图像:
var fileTransfer = new FileTransfer();
var uri = encodeURI("myserverurl/"+fileName);
var filePath = appCachePath+fileName;
fileTransfer.download(
uri,
filePath,
function(entry) {
alert("download complete: " + entry.fullPath);
},
function(error) {
alert("download error source/target/code:\n" + error.source +" \n||| "+error.target+" \n||| "+error.code);
}
);
我在成功获得 FS 后下载它们
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
if(device.platform === 'Android'){
cacheFolderSubPath = "Android/data/id.app.my/cache/";
}
else{
cacheFolderSubPath = ""; // for iOS what ??
}
}
function gotFS(fileSystem) {
var nturl = fileSystem.root.toNativeURL(); // returns cdvfile://localhost/persistent/ for both iOS and Android
window.resolveLocalFileSystemURL(nturl+cacheFolderSubPath, onResolveSuccess, onResolveFail);
}
function onResolveSuccess(fileEntry) {
// this is "file:///..." string mentioned in the question above.
appCachePath = fileEntry.toNativeURL()+"/";
// etc etc etc ... (use this appCachePath with download code above)
}
最佳答案
经过几天的努力,我找到了一个不太明显但非常简单的解决方案。
而不是使用
file:///var/mobile/Applications/<GUID of app>/Documents/
要在 iOS 上下载文件,我只使用普通的
cdvfile://localhost/persistent/
我从
fileSystem.root.toNativeURL(); // same for Android and iOS
这将我的文件成功下载到
file:///var/mobile/Applications/<GUID of app>/Documents/
这也是我用来在 HTML 中显示图像的 src 路径。
只是为了澄清我的情况:(使用 resolveLocalFileSystemURL() 时)
在 Android 上:
cdvfile://localhost/persistent/ -> file:///storage/sdcard0/
我必须添加
Android/com.my.appid/cache/
手动,如果我删除了应用程序,文件也会被删除,这是 好的。
在 iOS 上:
cdvfile://localhost/persistent/ -> file:///var/mobile/Applications/<GUID of app>/Documents/
这里我不需要添加任何东西,持久化存储已经 指向我的应用程序自己的“缓存”,在本例中是在文档中 文件夹。这也会随应用程序一起删除,这是正确的。
关于android - 应用程序缓存 iOS PhoneGap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24610313/