Cordova FileTransfer 下载 - 总是 returns 错误 3

Cordova FileTransfer Download - always returns error 3

我 运行 在使用 cordova 插件文件 T运行 时遇到了一些问题。那是我的代码:

window.requestFileSystem(
    LocalFileSystem.PERSISTENT,
    0,
    function onFileSystemSuccess(fileSystem) {
        fileSystem.root.getFile(
            "dummy.html", {create: true, exclusive: false},
            function gotFileEntry(fileEntry) {
                var sPath = fileEntry.fullPath.replace("dummy.html", "");
                var fileTransfer = new FileTransfer();
                fileEntry.remove();

                fileTransfer.download( 'http://cordova.apache.org/images/cordova_bot.png', sPath + photo.original_name,
                    function (theFile) {
                        alert('success: ' + JSON.stringify(theFile));
                        console.log("download complete: " + theFile.toURI());
                        // showLink(theFile.toURI());
                    },
                    function (error) {
                        alert('error: ' + JSON.stringify(error));
                        console.log("download error source " + error.source);
                        console.log("download error target " + error.target);
                        console.log("upload error code: " + error.code);
                    },
                    true
                );
            })
    },
    function (error) {
        alert('error request: ' + JSON.stringify(error));
    }
);

正在返回 fileTransfer.download 的错误回调,错误代码为 3,http 401。 我已经更新了 File 和 FileT运行sfer 插件,我的 cordova 版本是 4.3.0。 还检查了我的 config.xml

<access origin="*" />

但它就在那里。 我尝试添加 header 连接:关闭,但没有结果。 也尝试将下载的第 4 个参数设置为其默认值 (false) - 不走运。

正在 Android 平板电脑上测试。

有什么事吗?谢谢!

我个人不会创建一个文件然后删除它只是为了得到一个目录URL。您应该能够通过执行 fileSystem.root.toURL() - fileSystem.root 是一个 DirectoryEntry,因此包含您希望在 DirectoryEntry 上看到的方法。

再快一点。

更新

如果您倾向于使用文件删除方法,您应该在 FileEntry 上使用 toURL(),而不是 fullPath。我认为 toURL() returns 一个 URL 可以在整个 HTML app.

中使用

但正如我所说,fileSystem.root.toURL() 更可取。示例代码如下:

因此,您的代码变为:

window.requestFileSystem(
    LocalFileSystem.PERSISTENT,
    0,
    function (fileSystem) {
        var url = 'http://cordova.apache.org/images/cordova_bot.png',
            dir = fileSystem.root.toURL() + photo.original_name,
            ft = new FileTransfer();

        ft.download(url, dir,
            function (fileEntry) {
                alert('Downloaded!');
            },
            function (error) {
                alert('Download error');
                console.log(dir);
            }
        );
    },
    function (error) {
        alert('Error getting file system');
    }
);

试试看会发生什么。听起来可能很愚蠢,但我认为 photo.original_name 已定义?目录和文件名之间有一个'/'?

刚发现一个 "solution" 我的问题。 我所做的是将文件传输插件版本从 0.5.0 降级到 0.4.8。

如果有人遇到过类似问题,请按以下步骤操作:

  1. 通过运行'cordova plugins list'然后'cordova plugin remove name_from_list'
  2. 删除现有的文件传输插件
  3. 转到 https://github.com/apache/cordova-plugin-file-transfer/releases 并下载 0.4.8 版本的 zip 文件。
  4. 将文件解压缩到 cordova 应用程序的根目录中
  5. 运行 'cordova plugin add path_to_unzipped_folder'

就是这样。看起来还不错,至少返回了成功回调,没有真正测试更多。

如果您的远程文件需要 VPN 访问,请确保您的设备已连接到 VPN。

就我而言,我没有连接到设备上的 VPN。

把下载url改成 http://www.{{域名}}/abc.pdf

使用完整的 url 和 www

这对我有用

let pathToDownload = `${cordova.file.externalDataDirectory}`;
let fileTransfer   = new FileTransfer();    
let url            = encodeURI(`https://cordova.apache.org/images/cordova_bot.png`);

fileTransfer.download(url, pathToDownload + "image.png",
    (fileEntry) => {
        console.log('Downloaded!');
    },
    (error) => {
        console.log('Download error');           
    },                      
);