Cordova - 在下载文件夹中下载文件 [英] Cordova - Download a file in download folder

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

问题描述

我读过很多帖子,但没有最终答案。
从这个代码开始链接,我的文件下载到应用程序。无论如何,我想看到它的下载文件夹。
我正在使用Android,但显然我想要一个适用于iOS的解决方案。

I've read a lot of posts but I got no the FINAL answer. Starting from the code at this link, I got my file downloaded into the app. Anyway, I'd like to see it into the "Download" folder. I'm using Android, but clearly I'd like a solution valid for iOS also.

推荐答案

编辑

如果您已经知道文件的路径,您可以移动它:

If you already know the path of the file you can just move it:

var storageLocation = "";
console.log(device.platform);
switch (device.platform) {

    case "Android":
        storageLocation = 'file:///storage/emulated/0/';
        break;
    case "iOS":
        storageLocation = cordova.file.documentsDirectory;
        break;

}


var fileUri = "file:///data/user/0/com.arsdigitalia.myapp/files/files/MyApp‌​/test.pdf"

function moveFile(fileUri) {
    window.resolveLocalFileSystemURL(
          fileUri,
          function(fileEntry){

                var parentEntry = storageLocation + "Download";
               
                // move the file to a new directory and rename it
               fileEntry.moveTo(parentEntry, "newFile.pdf", success, fail);
                       
          },
          errorCallback);
}

原始

这是一个我用来完成这个的代码片段。它在Android上效果最好,iOS由于应用程序沙盒而有所不同,因此您需要自己处理检索文件。我还使用Cordova设备插件来确定应用程序正在运行什么设备,然后我可以更改存储路径以适应:

Here is a sample piece of code I use to accomplish this. It works best on Android, iOS is a bit different due to the app sandboxing so you need to handle retrieving files yourself. I also use the Cordova device plugin to determine what device the app is running on, I can then change storage paths to suit:

var storageLocation = "";
console.log(device.platform);
switch (device.platform) {

    case "Android":
        storageLocation = 'file:///storage/emulated/0/';
        break;
    case "iOS":
        storageLocation = cordova.file.documentsDirectory;
        break;

}

window.resolveLocalFileSystemURL(storageLocation,
    function (fileSystem) {

        fileSystem.getDirectory('Download', {
                create: true,
                exclusive: false
            },
            function (directory) {

                //You need to put the name you would like to use for the file here.
                directory.getFile("YOUR_FILE_NAME", {
                        create: true,
                        exclusive: false
                    },
                    function (fileEntry) {


                        fileEntry.createWriter(function (writer) {
                            writer.onwriteend = function () {
                                console.log("File written to downloads")
                            };

                            writer.seek(0);
                            writer.write(YOUR_FILE_HERE); //You need to put the file, blob or base64 representation here.

                        }, errorCallback);
                    }, errorCallback);
            }, errorCallback);
    }, errorCallback);

var errorCallback = function(e) {
    
    console.log("Error: " + e)
    
}

然后从您可以使用的目录中检索文件列表:

Then to retrieve the file list from the directory you can use:

window.resolveLocalFileSystemURL(storageLocation,
    function (fileSystem) {
    
        fileSystem.getDirectory('Download', {
                create: true,
                exclusive: false
            },
            function (directory) {

                var reader = directory.createReader();
                reader.readEntries(function (files) {

                    if (files.length == 0) {

                        console.log("No files found in downloads folder.")

                    } else {

                        $.each(files, function (i, v) {

                            console.log("File Name: " + files[i].name;)

                        });

                    }

                }, getFilesFail);
            }, getFilesFail);
    }, getFilesFail);

var getFilesFail = function(e) {
    
    console.log("Error: " + e);
    
}

要安装设备插件,请使用以下命令:

To install the device plugin use this command:

cordova plugin add cordova-plugin-device

这里的文档:

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-device/

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

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