Get checksum of a file in the browser using promises.
Complete documentation of the files can be found here.
First of all you need to include the Checksum class (either via ES6 imports or using CommonJS):
import Checksum from 'checksum-promise'
const Checksum = require('checksum-promise')
Then, all you need to do is instantiate the class (optionally passing in a config object):
const checksum = new Checksum()
Currently, the only option available in the config is the chunk size you want to divide your file by.
Optional config format:
{
chunkSize: 10485760
}
10485760
is the default value of the chunk size, therefore if you are ok with using this size, you don't need to supply a config whatsoever.
Finally, you can call the calculateMd5
method of the initialized object:
checksum.calculateMd5(file).then(checksum => {
// do whatever you need with the checksum
}).catch(error => {
// in case calculating the checksum failed
})
The provided file should be retrieved from an event on a file
input, like so:
function onFileInputChange (event) {
const file = event.target.files[0]
}
In case you want to contribute to this library, create a pull request on our BitBucket, stating what you changed.