Cordova File API将文件保存到存储卡

时间:2016-07-07 02:02:04

标签: javascript android file cordova fileapi

首先为什么在众神名称中没有教程,即使科尔多瓦的导游也没有告诉你如何将火焰文件保存到手机上的某个位置。

(function(w){
    function FileServices(FileName, callb){
        var self = this;
        var isFileOpen = false;
        var fileHandler;

        // open file handle
        function OpenFile(callb){
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 5*1024, function (fs) {
                fs.root.getFile(cordova.file.externalDataDirectory+FileName, { create: true, exclusive: false }, function (fileEntry) {
                    fileHandler = fileEntry;
                    isFileOpen = true;
                    callb(self, fileHandler);
                });
            }, function(e,c){console.log(e,c);});
        }

        // write to file
        function WriteFile(fileEntry, dataObj, isAppend, callback) {
            // Create a FileWriter object for our FileEntry (log.txt).
            fileEntry.createWriter(function (fileWriter) {
                fileWriter.onerror = function (e) {
                };

                // If we are appending data to file, go to the end of the file.
                if (isAppend) {
                    try {
                        fileWriter.seek(fileWriter.length);
                    }
                    catch (e) { }
                }
                fileWriter.write(dataObj);
                callback(self, fileWriter);
            });
        }

        this.writeLine = function(txt, append, callback){
            if(isFileOpen){
                WriteFile(fileHandler, new Blob([txt], { data:'plain/text' }), append, callback);
            }
        }

        OpenFile(callb);
    };
    // add to the navigator when device ready
    document.addEventListener("deviceready", function(){
        navigator.FileServices = function GetFileServices(FileName, callback){
            return new FileServices(FileName, callback);
        }
    });
})(window);

我得到的是"错误代码5 = FileError.ENCODING_ERR&#34 ;;

我无法解决这个问题....

0 个答案:

没有答案