How to write UTF-8 or Base64 data into a file (jpg/doc/pdf) on local storage(sdcard) in Phonegap

后端 未结 3 2065
渐次进展
渐次进展 2021-02-09 07:14

I am getting byte array like var byteArr=[12,-123,43,99, ...] from API, Then I am converting it into UTF-8 String by

     var utf8_str = String.fromCharCode.appl         


        
3条回答
  •  情深已故
    2021-02-09 07:46

    try this (make sure u are getting utf8_str properly):

    var utf8_str = String.fromCharCode.apply([], new Uint8Array(byteArr));
    var base64_str= window.btoa(utf8_str);
    function writeFile() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    }
    function gotFS(fileSystem) {
        fileSystem.root.getFile(FILE_NAME, {create: true}, gotFileEntry, fail);
    }
    function gotFileEntry(fileEntry) {
        fileEntry.createWriter(gotFileWriter, fail);
    }
    function gotFileWriter(writer) {
        writer.onwrite = function (evt) {
            alert('done');
        }
        writer.write(utf8_str);
    }
    function fail(error) {
        console.log(error.code);
    }
    

提交回复
热议问题