Blockchain Tierion API - 使用Node.js实时上传文件

时间:2018-05-29 12:25:06

标签: javascript node.js blockchain

主题:使用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)

1 个答案:

答案 0 :(得分:0)

我认为你在一个有http的网站上使用它? crypto api在不安全的域上不可用,您需要使用https进行摘要才能工作