ios - PhoneGap 将图像下载到特定文件夹

标签 ios cordova phonegap-plugins

我正在为 iOS 平台构建基于 PhoneGap 的应用程序。
我想将一些图像文件下载到特定目录。

我想将下载的图像存储在文件夹www/my_img/

这样在我的应用程序中我可以进一步将此图像用作:

<img src="my_img/downloaded.jpg" width="100px" height="100px">

我正在使用 PhoneGap 插件下载图像:

var url = 'http://myServer.com/img.jpg';
var filePath = 'www/my_img/';
var fileTransfer = new FileTransfer();
var uri = encodeURI(url);

fileTransfer.download(
    uri,
    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);
    },
    false,
    {
        headers: {
        }
    }
);

但问题是图像没有保存在指定的文件夹中。
如何将下载的图像保存在“www/my_img/”文件夹中?

最佳答案

问题出在 filePath 的值上。这需要是设备绝对文件路径文件系统URL

看看 comptibility notes part of the docs :

有一些您可以使用的预定义文件夹。我认为最适合您的情况(它是读/写,无需设置任何权限并且是持久的)是:

cordova.file.dataDirectory

您可以将下载的图像存储在那里,完成后设置图像源。

将其转化为您的案例:

HTML

<img id="downloadedimage" width="100px" height="100px">

JS

var url = 'http://myServer.com/img.jpg';
var filePath = cordova.file.dataDirectory + '/img.png';
var fileTransfer = new FileTransfer();
var uri = encodeURI(url);

fileTransfer.download(
    uri,
    filePath,
    function(entry) {
        console.log("download complete: " + entry.fullPath);
        document.getElementById("downloadedimage").src = 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: {
        }
    }
);

关于ios - PhoneGap 将图像下载到特定文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37808542/

相关文章:

ios - 在 iOS 键盘上方添加 UITextField

ios - UICollectionView 添加按钮到单元格

ios - 如何转换 iPhone 应用程序以支持 iPad

android - 适用于 Android 的 Card.io 或 Jumiophonegap/cordova 插件

javascript - Cordova 网络和相机 API 返回未定义

ios - Swift 中的 UIPickerView setter

android - Cordova /安卓 : change app banner color (NOT status bar)

jquery - 手机间隙 : Cannot multiple SQL request in same page

php - Pushwoosh Phonegap 通知 PHP 方法设置标签

cordova - phonegap 视频在 iOS 中不起作用