【问题标题】:FileTransfer Cordova download pathFileTransfer Cordova 下载路径
【发布时间】:2016-02-19 10:30:52
【问题描述】:

我正在使用 Cordova (5.4) 为 Android 和 Iphone 创建应用程序。一切都很好,除了我想使用 Cordova 的插件“FileTransfer”下载图像并且路径有一些问题。

如果我像这样使用 FileTransfer:

       uri = encodeURI('http://example.com/myImage.png'),
            fileURL = '/sdcard/Download/' + 'myImage.png',
fileTransfer.download(
                uri,
                fileURL,
                function (entry) {
                    console.log("download complete: " + entry.fullPath);
                },
                function (error) {
                    console.log(error);
                },
                false,
                {
                    headers: {
                        "authorization": 'Bearer ' + token
                    }
                }
            );

这很好用。但我想要一条适用于 Android 和 Iphone 的路径(不是静态路径),如果可以的话,用户可以直接在他们的图库中看到这些图像。

检查我尝试过的插件描述:

fileURL = 'cdvfile://localhost/persistent/myImg.png'

但这会因 FileTrasferError 而失败:

“/data/data/com.aco.plus/files/files/myImg.png:打开失败:ENOTDIR(不是目录)”

检查我也试过的答案:

uri = encodeURI('http://example.com/myImage.png');

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {

            fileTransfer.download(
                uri,
                fileSystem.root.toURL() + '/' + 'myImg.png',
                function (entry) {
                    console.log("download complete: " + entry.fullPath);
                },
                function (error) {
                    console.log(error);

                },
                false,
                {
                    headers: {
                        "authorization": 'Bearer ' + token
                    }
                }
            );
        });

我也遇到了同样的错误。

我很迷茫。有谁知道我能做什么?我很确定这一定是比静态路由更好的方法。

【问题讨论】:

  • 我知道这是一个很老的问题,但你能解决这个问题吗?因为我在将文件保存到下载文件夹时也遇到了麻烦。
  • 如果下载成功,您应该重新扫描您的设备存储,因为 Cordova 不知道文件是否已下载。所以我做了一个插件,它是一个下载后更新画廊的插件。 cordova-plugin-gallery-refresh

标签: android iphone cordova


【解决方案1】:

@Luisma,

请找到示例代码sn-p使用cordova文件和文件传输插件在设备中写入pdf文件:

var fileTransfer = new FileTransfer();

if (sessionStorage.platform.toLowerCase() == "android") {
    window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemSuccess, onError);
} else {
    // for iOS
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
}

function onError(e) {
    navigator.notification.alert("Error : Downloading Failed");
};

function onFileSystemSuccess(fileSystem) {
    var entry = "";
    if (sessionStorage.platform.toLowerCase() == "android") {
        entry = fileSystem;
    } else {
        entry = fileSystem.root;
    }
    entry.getDirectory("Cordova", {
        create: true,
        exclusive: false
    }, onGetDirectorySuccess, onGetDirectoryFail);
};

function onGetDirectorySuccess(dir) {
    cdr = dir;
    dir.getFile(filename, {
        create: true,
        exclusive: false
    }, gotFileEntry, errorHandler);
};

function gotFileEntry(fileEntry) {
    // URL in which the pdf is available
    var documentUrl = "http://localhost:8080/testapp/test.pdf";
    var uri = encodeURI(documentUrl);
    fileTransfer.download(uri, cdr.nativeURL + "test.pdf",
        function(entry) {
            // Logic to open file using file opener plugin
        },
        function(error) {
            navigator.notification.alert(ajaxErrorMsg);
        },
        false
    );
};

【讨论】:

  • 嗨,甘地,如果您发现重复项,请将它们标记为此类。
  • @bummi,你能指导我吗,因为我对此很陌生?提前致谢
  • 这个问题和你answered too 的问题共享相同的答案,这可能表明应该关闭作为另一个问题的副本。我不熟悉科尔多瓦,所以我没有标记/投票。如果您认为它们是重复的,只需将一个标记为更好的重复。选择标志/重复和目标的链接。
  • @bummi,感谢您的宝贵时间和详细的回复。
  • 非常感谢@Gandhi 的帮助。按照您的代码作为指南,我设法在 iPhone 上保存了一个存档。
【解决方案2】:

对于进入应用程序的路径,我喜欢使用

https://github.com/apache/cordova-plugin-file

这会映射每个操作系统上的不同路径,因此它对您来说是透明的,即使通过不同的 SO 或版本,它也会选择正确的路径。

编码愉快!

【讨论】:

  • 非常感谢您的帮助,对于延迟发表评论,我们深表歉意。我已经在使用你说的插件,但我认为我做错了什么或者我错过了一些东西:例如,当我使用 cordova.file.applicationStorageDirectory + '/' + fName 时,它​​说它存储文件在“ /data/data/com.aco.plus/myImg.jpg”中,但没有 /data/data 目录,并且图像不在文件系统中(为什么 /data/data? 似乎重复)。如果我使用“cdvfile://localhost/persistent/Download/”+ fName 之类的东西,我会得到一个 FileTrasferError,就像我之前展示的那样。我做错了什么?
猜你喜欢
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 2015-06-20
  • 1970-01-01
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多