简体   繁体   English

从 Cordova iOS 应用程序从网站下载文件

[英]Downloading a file from a website from a Cordova iOS app

I have a Cordova app that downloads an image file from a website and saves to the local cache.我有一个 Cordova 应用程序,它从网站下载图像文件并保存到本地缓存。 This works fine on Android, but not on iOS.这适用于 Android,但不适用于 iOS。 I am using Cordova 10.0.0 with iOS 6.1.1我正在使用 Cordova 10.0.0 和 iOS 6.1.1

var xhr = new XMLHttpRequest();
xhr.open("GET", 'https://www.mywbsite.com/image.jpg', true);
xhr.responseType = "blob";

xhr.onreadystatechange = function () {
    console.log(xhr.readyState);
}

xhr.onload = function() {
    
    var blob = xhr.response;

    window.resolveLocalFileSystemURL(cordova.file.cacheDirectory, function (directoryEntry) {
        directoryEntry.getFile('downloadedFile.jpg', { create: true }, function (fileEntry) {
            fileEntry.createWriter(function (fileWriter) {
                fileWriter.onwriteend = function (result) {
                    console.log("Success");
                };
                fileWriter.onerror = function (e) {
                    console.log("failed");
                };
                fileWriter.write(blob);
            }, function(e) {
                console.log("failed");
            });
        }, function(e) {
            console.log("failed");
        });
    }, function(e) {
        console.log("failed");
    });
}
xhr.onerror = function(e) { 
    console.log("failed "+xhr.status);
}

xhr.send();

The readyState goes right to 4 and the status is 0. I suspect this has something to do with WKWebView, but I am unsure how to resolve this. readyState 变为 4,状态为 0。我怀疑这与 WKWebView 有关,但我不确定如何解决这个问题。 The remote site is not running a script, just the direct image I want to download.远程站点没有运行脚本,只是我要下载的直接图像。

Thanks!谢谢! Jon乔恩

I found the answer to my issue.我找到了我的问题的答案。 Installing the plugin cordova-plugin-wkwebview-file-xhr allowed WKWebview to establish the connection and download the file.安装插件 cordova-plugin-wkwebview-file-xhr 允许 WKWebview 建立连接并下载文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM