ios - 用于将文件下载到 iOS 的 Cordova 文件传输插件

标签 ios cordova cordova-plugins phonegap file-transfer

我正在使用以下代码下载文件并将其保存到我的手机存储空间中。

    var downloadUrl = encodeURI(cordova.file.dataDirectory + fileName);
    var fileTransfer = new FileTransfer();

    fileTransfer.download(
    "http://developer.android.com/assets/images/home/ics-android.png",
    downloadUrl,
    function(entry) {
    alert("download complete: " + entry.fullPath);
    },

    function(error) {
    alert("download error source " + error.source);
    alert("download error target " + error.target);
    alert("upload error code" + error.code);
    });

此代码在任何 Android 设备上都可以正常工作,但在 iPhone 中不起作用,会出现以下错误。

    download error source " the url used"
    download error target: " the target used "
    upload error code 1

我是 iOS 新手,有人可以帮我解决这个问题吗?并让我知道在执行此代码之前应授予的任何权限...

最佳答案

文件传输插件已被弃用一段时间(超过一年),您可能不应该花太多时间让文件传输插件正常工作。我建议按照博客文章中关于如何 transition off the file api 的说明进行操作。 .

因此对于您的代码(未经测试,但应该可以工作):

window.requestFileSystem(cordova.file.dataDirectory, 0, function (fs) {
    console.log('file system open: ' + fs.name);
    fs.root.getFile(fileName, { create: true, exclusive: false }, function (fileEntry) {
        console.log('fileEntry is file? ' + fileEntry.isFile.toString());
        var oReq = new XMLHttpRequest();
        // Make sure you add the domain name to the Content-Security-Policy <meta> element.
        oReq.open("GET", "http://developer.android.com/assets/images/home/ics-android.png", true);
        // Define how you want the XHR data to come back
        oReq.responseType = "blob";
        oReq.onload = function (oEvent) {
            var blob = oReq.response; // Note: not oReq.responseText
            if (blob) {
                fileEntry.createWriter(function (fileWriter) {

                    fileWriter.onwriteend = function() {
                        console.log("Successful file write...");                        
                    };

                    fileWriter.onerror = function (e) {
                        console.log("Failed file write: " + e.toString());
                    };

                    fileWriter.write(blob);
                });
            } else {
                console.error('we didnt get an XHR response!');
            }
        };
        oReq.send(null);
    }, function (err) { 
        console.error('error getting file! ' + err); 
    });
}, function (err) { 
    console.error('error getting persistent fs! ' + err); 
});

关于ios - 用于将文件下载到 iOS 的 Cordova 文件传输插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53968281/

相关文章:

ios - ionic 获取 ios 的设备信息不起作用,而相同的代码适用于 Android

android - ionic 文件下载不起作用

iphone - 在 ios6 中解析 csv

ios - animateKeyframesWithDuration 使屏幕在动画后没有响应

ios - 上传的 IPA 不会出现在 iOS 8 Test Flight 应用程序中。他们改变程序了吗?

javascript - 使用本地存储进行应用内购买和应用更新

cordova - 删除未使用的 cordova 插件

ios - FaSTLane:将设备添加到配置的正确方法?

升级 Cordova 的相机插件时 Android 构建错误

css - 如何解决 Bootstrap 类名冲突