离子文件传输错误

Ionic file transfer error

我正在使用 cordova 文件传输和文件插件转到服务器并获取一些数据和图像。然后我将这些数据保存到设备的存储中,数据格式是 .json 文件和一些图像。我可以将所有数据保存到设备中。

当我尝试使用该数据时出现问题。当我尝试加载本地数据时,我得到

Failed to load resource: net::ERR_FILE_NOT_FOUND file:///android_asset/www/Android/data/com.mydomain.myApp/files/data.json

从控制台日志消息我可以看到 'file:///android_asset/www/' 在被调用时被添加到路径中,这是正确的吗?

我正在使用 angularjs $resource 服务。

var localData = $resource(filePath);

如果有人能提供帮助,我将不胜感激。

您确定将文件存储在正确的文件夹中吗?

阅读 Cordova File Plugin documentation 你会得到这个:

Where to Store Files

As of v1.2.0, URLs to important file-system directories are provided. Each URL is in the form file:///path/to/spot/, and can be converted to a DirectoryEntry using window.resolveLocalFileSystemURL().

cordova.file.applicationDirectory - Read-only directory where the application is installed. (iOS, Android, BlackBerry 10)

cordova.file.applicationStorageDirectory - Root directory of the application's sandbox; on iOS this location is read-only (but specific subdirectories [like /Documents] are read-write). All data contained within is private to the app. ( iOS, Android, BlackBerry 10)

cordova.file.dataDirectory - Persistent and private data storage within the application's sandbox using internal memory (on Android, if you need to use external memory, use .externalDataDirectory). On iOS, this directory is not synced with iCloud (use .syncedDataDirectory). (iOS, Android, BlackBerry 10)

cordova.file.cacheDirectory - Directory for cached data files or any files that your app can re-create easily. The OS may delete these files when the device runs low on storage, nevertheless, apps should not rely on the OS to delete files in here. (iOS, Android, BlackBerry 10)

cordova.file.externalApplicationStorageDirectory - Application space on external storage. (Android)

cordova.file.externalDataDirectory - Where to put app-specific data files on external storage. (Android)

cordova.file.externalCacheDirectory - Application cache on external storage. (Android)

cordova.file.externalRootDirectory - External storage (SD card) root. (Android, BlackBerry 10)

cordova.file.tempDirectory - Temp directory that the OS can clear at will. Do not rely on the OS to clear this directory; your app should always remove files as applicable. (iOS)

cordova.file.syncedDataDirectory - Holds app-specific files that should be synced (e.g. to iCloud). (iOS)

cordova.file.documentsDirectory - Files private to the app, but that are meaningful to other application (e.g. Office files). (iOS)

cordova.file.sharedDirectory - Files globally available to all applications (BlackBerry 10)

基于此列表,我的建议是您通常将文件存储在 cordova.file.dataDirectory 中,这样做:

var targetPath = cordova.file.dataDirectory + filename + extension
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");

fileTransfer.download(
    uri,
    targetPath,
    function(entry) {
        console.log("download complete: " + entry.toURL());
    },
    function(error) {
        console.log("download error source " + error.source);
        console.log("download error target " + error.target);
        console.log("upload error code" + error.code);
    },
    false,
    {
        headers: {
            "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
        }
    }
);

它与 iOS 和 Android 都兼容。

例如,您可以使用 targetPath 变量打开下载的图像:

//On your controller assign it to the scope
$scope.targetPath = targetPath;

//then on your view just use it as the src for your img
<img ng-src="targetPath">

好的,所以我做错了。因为数据是本地的,所以我不应该发出 $resource 请求。我改为使用 ngCordova documentation 中的 readAsText(path, file)

本质上,当我将数据保存到 .json 文件时,它被视为文本文件,然后我可以将方法与 JSON.parse(data); 结合使用,然后可以设置到我的范围并以与远程数据相同的方式使用。

P.S。我已经尝试使用 'cordova.file.dataDirectory' 和 'cordova.file.externalDataDirectory' 并且都在我的测试中工作。 'Fabio Antunes' 在这个 post 中提供的信息值得一读。