Cordova - 下载文件到下载文件夹

10
我已经阅读了很多文章,但是没有得到最终答案。从这个链接的代码开始,我已经将文件下载到应用程序中。但是,我想把它放到“下载”文件夹中。我使用的是Android,但显然我希望iOS也能使用这个解决方案。
2个回答

15

编辑

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

最初的回答:

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设备插件来确定应用程序运行的设备,然后可以更改存储路径以适应:

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

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

最初的回答:

dir /b

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

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

cordova plugin add cordova-plugin-device

文档在这里:

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

"Original Answer"翻译成"最初的回答"。

对于写入行 writer.write(YOUR_FILE_HERE);,我认为我不能使用文件路径作为 YOUR_FILE_HERE。实际上,我有:file:///data/user/0/com.arsdigitalia.myapp/files/files/MyApp/test.pdf,但原始的 400kB 文件没有被正确复制(我在下载文件夹中找到了一个 80B 的文件)。有什么建议吗? - Zappescu
好的,我使用了官方插件文档中解释的readBinaryFile(https://github.com/apache/cordova-plugin-file-transfer#download-a-binary-file-to-the-application-cache-),并成功获取了文件。只是最后一个问题:我通过文件管理器访问并查看下载文件夹中的文件,但在Android 7.1的“官方”下载文件夹(桌面上的图标)中却看不到它。为什么? - Zappescu
我编辑了我的帖子,加入了一个函数,应该可以将文件从一个目录移动到另一个目录。我认为你在官方下载应用程序中看不到它的原因是它没有被Android下载管理器下载到那个文件夹中。 - L Balsdon
好的,但是你应该将你的函数改成这个:window.resolveLocalFileSystemURL( fileUri, function (fileEntry) { var parentEntry = storageLocation + "Download"; window.resolveLocalFileSystemURL(parentEntry, function (store) { // 将文件移动到新目录并重命名 fileEntry.moveTo(store, "newFile.pdf", function () { console.log("移动完成"); }, function () { console.log("移动错误"); }); },errorCallback); }, errorCallback); - Zappescu
2
你是说Android的下载文件夹在file:///storage/emulated/0/Download吗? - João Pimentel Ferreira
1
这还管用吗? 我认为对于iOS,您正在将文件移动到应用目录路径内的“下载”文件夹中 - 这意味着用户仍然无法在其iPhone的通用“下载”文件夹中看到它。 为什么苹果要这么难搞。 您可以将文件下载到用户手机上,但用户无法看到它们。 - rolinger

0
注意:对于想在iOS的iTunes和文件应用中查看已下载文件的用户,您需要在info.plist文件中同时启用UIFileSharingEnabledLSSupportsOpeningDocumentsInPlace两个选项。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接