file - Cordova - 下载下载文件夹中的文件

标签 file cordova download

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

最佳答案

编辑

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

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

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

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/

关于file - Cordova - 下载下载文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43575581/

相关文章:

c - 我试图从 c 中的文件中读取一行并动态分配内存,但结果总是不好

java - 使用 RandomAccessFile 到达文件中的特定行

java - WebChromeClient 无法转换为 org.apache.cordova.CordovaChromeClient

Android/Cordova UUID 可靠性

python - 在 Python 中使用 pathlib 复制文件

c - 程序不会停止读取文件

ios - phonegap (cordova) 是否支持使用声音捕获视频?

jsf-2 - p :fileDownload in p:dataTable does not work (just refreshes page) after performing search on the p:dataTable

download - 将视频保存到 CameraRoll React-Native

java - 文件下载时返回字节数组或 servlet 输出流之间有区别吗