chrome only filereader bug

时间:2018-05-30 07:40:49

标签: javascript html5 google-chrome filereader

我正在尝试读入文件并计算SHA-256哈希值。 该代码完全适用于localhost(Chrome和Safari)。

只要我在网络服务器上传页面,我就会在Google Chrome中收到以下错误:

  

未捕获的TypeError:无法读取属性'摘要'未定义的       在FileReader.reader.onload(hashDocument.js:40)

但是,该网页在Safari和Android设备上都能正常运行。

function convertStringToArrayBufferView(str)
{
    var bytes = new Uint8Array(str.length);
    for (var iii = 0; iii < str.length; iii++)
    {
        bytes[iii] = str.charCodeAt(iii);
    }
    return bytes;
}

function convertArrayBufferToHexaDecimal(buffer)
{
    var data_view = new DataView(buffer);
    var iii, len, hex = '', c;

    for(iii = 0, len = data_view.byteLength; iii < len; iii += 1)
    {
        c = data_view.getUint8(iii).toString(16);
        if(c.length < 2)
        {
            c = '0' + c;
        }
        hex += c;
    }
    return hex;
}

function hashIt() {
  var nBytes = 0,
      oFiles = document.getElementById("documentIn").files,
      nFiles = oFiles.length;
  for (var nFileId = 0; nFileId < nFiles; nFileId++) {
      //console.log(oFiles[nFileId]);

      var reader = new FileReader();

      reader.onload = function(e) {
      var text = reader.result;
      var promise = crypto.subtle.digest({name: "SHA-256"},   convertStringToArrayBufferView(text));

    promise.then(function(result){
      var hashValue = convertArrayBufferToHexaDecimal(result);
      // update input field
      console.log("hashValue");
      $("#documentHash").val(hashValue);
    });

};

reader.readAsText(oFiles[nFileId]);

    nBytes += oFiles[nFileId].size;
  }
}

0 个答案:

没有答案