主题:使用Node.js
大家好,
我正在使用upload
处理真实文件Node.js
,我在下面提到的代码中遇到错误:
function handleFile(files) {
if (!files.length) {
return;
}
var file = files[0];
var reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onprogress = function (e) {
dragZone.removeClass( 'is-uploading' );
dragZone.removeClass( 'is-error' );
dragZone.addClass( 'is-success' );
if (e.lengthComputable) {
var percentComplete = (e.loaded / e.total)*100;
percentComplete = parseInt(percentComplete);
//Do something with upload progress
console.log(percentComplete);
$('#status').text(percentComplete+'%');
$('.progress-bar').text(percentComplete+'%');
$('#progressBar').val(percentComplete);
//console.log(e.loaded+ " / " + e.total)
}
}
reader.onload = function(e) {
var data = e.target.result;
//Error-facing
window.crypto.subtle.digest({name: 'SHA-256'}, data).then(function(hash) {
var hexString = '';
var bytes = new Uint8Array(hash);
for (var i = 0; i < bytes.length; i++) {
var hex_i = bytes[i].toString(16);
hexString += hex_i.length === 1 ? '0' + hex_i : hex_i;
}
$('#hash').text(hexString);
calculatedHash = hexString;
dragZone.removeClass( 'is-uploading' );
dragZone.removeClass( 'is-error' );
dragZone.addClass( 'is-success' );
}).catch(function(e) {
showError(e);
});
};
任何人都可以指导我如何解决此错误:
index.js:138 Uncaught TypeError: Cannot read property 'digest' of undefined
at FileReader.reader.onload
下面提到的行没有数据传递给这个哈希变量:
window.crypto.subtle.digest({name: 'SHA-256'}, data).then(function(hash)
答案 0 :(得分:0)
我认为你在一个有http的网站上使用它? crypto api在不安全的域上不可用,您需要使用https进行摘要才能工作