FileTransfer Cordova 下载路径 [英] FileTransfer Cordova download path

查看:35
本文介绍了FileTransfer Cordova 下载路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Cordova (5.4) 为 Android 和 Iphone 创建应用程序.一切正常,除了我想使用 Cordova 的插件FileTransfer"下载图像我在路径上遇到了一些问题.

I'm using Cordova (5.4) to create apps for Android and Iphone. All goes fine, except I want to download images using the Cordova's plugin "FileTransfer" and I having some problems with the path.

如果我像这样使用 FileTransfer:

If I use the FileTransfer like this:

       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 的路径(不是静态路径),如果可以的话,用户可以直接在他们的图库中看到这些图像.

This works fine. But I would want a path that worked on Android and Iphone, (not a static one) and if it could be, that the user could see this images directly in their gallery.

检查我试过的插件描述:

Checking the plugin description I tried:

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

但是这失败了 FileTrasferError:

But this fails with the FileTrasferError:

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

"/data/data/com.aco.plus/files/files/myImg.png: open failed: ENOTDIR (Not a directory)"

检查周围的答案我也试过:

Checking answers around I tried also:

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
                    }
                }
            );
        });

我也遇到了同样的错误.

And I got the same error.

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

I'm quite lost. Anyone knows what can I do? I'm quite sure that must be a better way to do it than static routes.

推荐答案

@Luisma,

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

Please find the sample code snippet to write pdf file in device using cordova file and file transfer plugin:

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
    );
};

这篇关于FileTransfer Cordova 下载路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆