科尔多瓦文件插件十六进制字符串写入错误

时间:2019-12-28 17:22:39

标签: file cordova plugins hex

我使用cordova-file-plugin将十六进制字符串写入文件。创建文件后,当我想将文件解析为在线文件十六进制转换器上的十六进制字符串时,我看到一些十六进制字节是错误的。我找不到我的错。 例如,我想写“ 93”十六进制字符串,但我在在线文件十六进制转换器上看到201c。 这是我的代码:

pdfgoster(hexstring);
function pdfgoster(data){
    var folderpath=cordova.file.externalRootDirectory;
    var contentType = "type: 'text/plain'";

    var filename = "merhaba1.ddd";

        olustur(folderpath,filename,data);
}
function olustur(folderpath,filename,DataBlob) {
  var contentType = "type: 'text/plain'";
  var blob=toPngBlob(DataBlob,contentType);
 window.resolveLocalFileSystemURL(folderpath, function(dir) {
                console.log("Access to the directory granted succesfully");
                dir.getFile(filename, {create:true}, function(file) {
                    console.log("File created succesfully.");
                    file.createWriter(function(fileWriter) {
                        console.log("Writing content to file");


                        fileWriter.write(blob);
                        //navigator.notification.alert("Finished writing file.");
                        //open file
                       // cordova.plugins.fileOpener2.open(folderpath + filename, 'application/pdf');
                    }, function(){
                        //navigator.notification.alert('Unable to save file in path '+ folderpath);
                    });
                });
            });


}
function toPngBlob(str,s){
    var hexStr = str.slice(2);
    var buf = new ArrayBuffer(hexStr.length/2);
    var byteBuf = new Uint8Array(buf);
    for (let i=0; i<hexStr.length; i+=2) {
      byteBuf[i/2] = parseInt(hexStr.slice(i,i+2),16);
    }
    var blob = new Blob([byteBuf], {type: 'text/plain'});
    return blob;
  }

0 个答案:

没有答案