Cordova - Save a file to storage

我与影子孤独终老i 提交于 2019-12-11 16:33:17

问题


I am making a Cordova application from which I need to export a file. I would like to save the file to the Android device's storage: /storage/emulated/0/. The app should create a folder in which it will create a file with content in it.

I tried the cordova-plugin-file plugin but I'm not sure how to use it. There are examples on the plugin's documentation but I don't know which one to use, there is:

  • Create a persistent file
  • Write to a file
  • Append a file using alternative methods

And I tried them all however none of them works.

Your help and an example (if possible) would be greatly appreciated.

EDIT

There's the code I used. I'm not getting any error.

function createFile(dirEntry, fileName, fileContent, isAppend) {
    dirEntry.getFile(fileName, {create: true, exclusive: false}, function(fileEntry) {
        writeFile(fileEntry, fileContent, isAppend);
    }, fail);
}

function savePasswords(fileSystem) {
    createFile("/sdcard/testFolder", "testfile.txt", "TEST", true);
}

function fail(error) {
    alert("ERROR: " + error.code);
}

function request() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, savePasswords, fail);
}

document.addEventListener("deviceready", request, false);

I want this to create the file "testfile.txt" with content "TEST" in a folder named "testFolder".


回答1:


Since Android 4.4, the SD card root (/sdcard/) is read-only so you cannot write to it. Assuming your reference to writeFile() in your example code refers to the cordova-plugin-file example (since it's not defined in your code), then the fileWriter.onerror() function would be invoked with error code NO_MODIFICATION_ALLOWED_ERR.

You must write to the application storage directory on the SD card (e.g. /sdcard/Android/data/your.app.package.id/). You can reference this location using cordova-plugin-file as cordova.file.externalApplicationStorageDirectory.

See this answer for details of SD card access in different versions of Android.

Note: above references to "SD card" refer to the emulated SD card (on internal memory (i.e. /storage/emulated/0/). Referencing the external/removable SD card present in some Android devices (e.g. Samsung Galaxy S range) is not possible via cordova-plugin-file, however you can use getExternalSdCardDetails() from cordova-diagnostic-plugin to do so.




回答2:


This script works:

function writeFile(fileEntry, dataObj) {
    // Create a FileWriter object for our FileEntry (log.txt).
    fileEntry.createWriter(function (fileWriter) {

        fileWriter.onwriteend = function() {
            console.log("Successful file write...");
            readFile(fileEntry);
        };

        fileWriter.onerror = function (e) {
            console.log("Failed file write: " + e.toString());
        };

        // If data object is not passed in,
        // create a new Blob instead.
        if (!dataObj) {
            dataObj = new Blob(["Content if there's nothing!"], { type: 'text/plain' });
        }

        fileWriter.write(dataObj);
    });
}

window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, function (rootDirEntry) {
        rootDirEntry.getDirectory(fileDir, { create: true }, function (dirEntry) {
            var isAppend = true;
            dirEntry.getFile(fileName, { create: true }, function (fileEntry) {
                writeFile(fileEntry, "Content!", isAppend);
                // Success
            });
        });
    });


来源:https://stackoverflow.com/questions/47846958/cordova-save-a-file-to-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!