首页 文章

Cordova file-transfer 插件无法在模拟器中运行

提问于
浏览
0

我正在尝试获取 file-transfer 插件的示例代码,它直接来自 Cordova 文档:

function downloadFile2() {
    window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {

        console.log('file system open: ' + fs.name);

        // Make sure you add the domain name to the Content-Security-Policy <meta> element.
        var url = 'http://cordova.apache.org/static/img/cordova_bot.png';
        // Parameters passed to getFile create a new file or return the file if it already exists.
        fs.root.getFile('downloaded-image.png', { create: true, exclusive: false }, function (fileEntry) {
            download2(fileEntry, url, true);

        }, function () { logError('Error creating file'); });

    }, function () { logError('Error creating fs'); });
}

function download2(fileEntry, uri, readBinaryData) {

    var fileTransfer = new FileTransfer();
    var fileURL = fileEntry.toURL();

    console.log('Downloading ' + uri + ' to ' + fileURL);
    fileTransfer.download(
        uri,
        fileURL,
        function (entry) {
            console.log("Successful download...");
            console.log("download complete: " + entry.toURL());
            if (false && readBinaryData) {
                // Read the file...
                readBinaryFile(entry);
            }
            else {
                // Or just display it.
                displayImageByFileURL(entry);
            }
        },
        function (error) {
            console.log("download error source " + error.source);
            console.log("download error target " + error.target);
            console.log("upload error code" + error.code);
        },
        null, // or, pass false
        {
            //headers: {
            //    "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
            //}
        }
    );
}

function displayImageByFileURL(fileEntry) {
    var elem = document.getElementById('imageElement');
    elem.src = fileEntry.toURL();
}

我正在使用最新版本的 file-transfer 和文件插件(1.7.1/6.0.1)。我已将域添加到 Content-Security-Policy 元素中,如示例中所述:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: http://cordova.apache.org https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">

当我在 VS2017 的模拟器(Android/iOS)中运行它时,下载无声地失败。既不会调用成功或错误回调,也不会生成网络请求。控制台日志如下:

file system open: http_localhost_4400:Temporary
Downloading http://cordova.apache.org/static/img/cordova_bot.png to filesystem:http://localhost:4400/temporary/downloaded-image.png

那个文件系统 URL 对我来说有点奇怪,所以我尝试了其他变种,比如完整的文件路径,使用持久存储而不是临时存储,使用'cdvfile://localhost/persistent/downloaded-image.png',都具有相同的结果。我不知道如何进一步调试这个,并想知道我是否错过了一些非常明显的东西,所以任何建议都赞赏...

编辑我今天尝试再次运行它,并在 Visual Studio 中添加了一个对话框,其中包含以下消息:

以下 exec 调用没有处理程序:
FileTransfer.download(“http://cordova.apache.org/static/img/cordova_bot.png”,“cdvfile://localhost/persistent/downloaded-image.png”,true,1,null)

1 回答

  • 0

    我做了一些实验,包括在 Android 的 VS 模拟器中运行它。由于某种原因,它无法连接到 cordova.apache.org(我也无法在模拟器上的浏览器中访问此站点),但从 github 下载文件工作正常....

相关问题