How to checksum the file to be uploaded with javascript?

丶灬走出姿态 提交于 2019-12-22 13:49:11

问题


I want to checksum the files at browser side before uploading, then checksum and compare at server side to make sure the consistent. But how can I get the pure binary data of the file and checksum it? I tried the way below, but doesn't work:

let fileSelect = document.getElementById('file')
let files = fileSelect.files
let file = files[0]

var r = new FileReader();
r.onload = function(){ console.log(r.result); };
r.readAsArrayBuffer(file);
var file_sha1 = sha1(r.result)

回答1:


The library you are using seems to support string input only. Find a library support binary input. Eg. js-sha1. And you should hash it in the callback.

var reader = new FileReader();
reader.onload = function (event) {
  var file_sha1 = sha1(event.target.result)
};
reader.readAsArrayBuffer(file);


来源:https://stackoverflow.com/questions/36177968/how-to-checksum-the-file-to-be-uploaded-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!