Javascript FileReader错误地读取文件

时间:2017-09-18 22:21:01

标签: javascript ecmascript-6 filereader iso-8859-1 windows-1252

我有一个简短的JavaScript函数,它将获取一个上传的文件并显示它的十六进制等效值。在十六进制编辑器中比较原始文件和输出显示它们部分不同但不完全。

String.prototype.hexEncode = function(){
  var hex, i;

  var result = "";
  for (i = 0; i < this.length; i++) {
    hex = this.charCodeAt(i).toString(16);
    result += ("" + hex).slice(-4);
  }

  return result
}

function upload() {
  var file = document.getElementById("fileToUpload").files[0];
  var reader = new FileReader();
  reader.readAsText(file, "windows-1252");
  reader.onload = function (evt) {
    var program = evt.target.result.hexEncode();
    program = program;
    console.log(program);
  }
}

以下是原始文件和彼此相邻的输出:

2A 2A 54 49 38 33 46 2A 1A 0A 0A 43 72 65 61 74
2A 2A 54 49 38 33 46 2A 1A AA 43 72 65 61 74 65

导致产出差异的原因是什么?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

要在js中生成十六进制转储,您不希望使用readAsText方法,该方法将转换UCS-2或UTF-16中的数据,而是直接读取您的二进制数据我将从readAsArrayBuffer方法获取,并从那里开始工作:

function hexDump(file) {
  return new Promise((res, rej) => {
    if (!(file instanceof Blob)) rej('wrong input');
    const reader = new FileReader();
    reader.onload = e => {
      res(hex(reader.result));
    };
    reader.onerror = e => rej('error while reading');
    reader.readAsArrayBuffer(file);
  });
  // gotten from https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#Example
  function hex(buffer) {
    const hexCodes = [];
    const view = new DataView(buffer);
    for (let i = 0; i < view.byteLength; i += 4) {
      // Using getUint32 reduces the number of iterations needed (we process 4 bytes each time)
      let value = view.getUint32(i)
      // toString(16) will give the hex representation of the number without padding
      let stringValue = value.toString(16)
      // We use concatenation and slice for padding
      let padding = '00000000'
      let paddedValue = (padding + stringValue).slice(-padding.length).toUpperCase();
      hexCodes.push( // simple prettyfying
        paddedValue.slice(0,2),
        paddedValue.slice(2,4),
        paddedValue.slice(4,6),
        paddedValue.slice(6,8)
        );
    }
    return hexCodes.join(' ');
  }
}

// How to use it
inp.onchange = e => hexDump(inp.files[0].slice(0, 100)) // for demo I slice the file
    .then(hex => console.log(hex))
    .catch(e => console.error(e));
<input type="file" id="inp">