Background:
The first time I used Spark-MD5.js was almost a year ago. At that time, the backend boss said that he would build a file shard upload function. I was like, what? What is file shard upload?
As for my inner panic at that time will not be described, in short, file sharding upload needs a unique identifier to identify the file, and MD5 is very suitable. Spark-md5. js is a reliable solution for the front-end to calculate MD5 locally before uploading files
Spark – md5. Js was written by foreigners, if English foundation is good and would like to know more information can understand to NPM website: www.npmjs.com/package/spa…
Use:
The first thing to do is to introduce spark-md5.js into the HTML file. You can introduce the compressed version or the development version according to your needs
I should mention that I’m using the html5 FileReader interface, which is currently implemented by FireFox3.6+, chrome6+, and IE10+, so if more compatibility is needed, I’m sorry, but I haven’t found it yet
About FileReader, there is a high quality blog may I have an idea: blog.csdn.net/jackfrued/a…
Two methods are provided here; One is to use sparkmd5.hashBinary () to pass the binary of the entire file directly into the MD5 of the direct-return file. This method is advantageous for small files — it is simple and fast.
The other method is to slice(file.prototype.slice ()) the File object in JS and pass it into spark.appendbinary () one by one to calculate and output the result with spark.end(). This approach can be very beneficial for large files — it is less error-prone and provides information about the progress of the computation
Let’s get started. Here’s the code:
First of all, the first method:
var running = false; //running Indicates whether MD5 is being calculatedfunction doNormalTest(input) {// It is assumed that a DOM reference to the file selection box is passed in directlyif(running) {// If the next calculation is being performed, the next calculation is not allowed to startreturn;
}
var fileReader = new FileReader(), //创建FileReader实例
time;
fileReader.onload = function(e) {//FileReader load event, running = triggered when the file is finished readingfalse; // e.target points to the fileReader instance aboveif(file.size ! = e.target.result.length) {// If the two are inconsistent, the reading error alert("ERROR:Browser reported success but could not read the file until the end.");
} else{ console.log(Finished loading! success!!) ;returnSparkMD5.hashBinary(e.target.result); // calculate md5 and return result}}; fileReader.onerror =function() {// If there is an error in reading the file, cancel reading state and popup box error running =false;
alert("ERROR:FileReader onerror was triggered, maybe the browser aborted due to high memory usage.");
};
running = true; fileReader.readAsBinaryString( input.files[0] ); // Use fileReader to read file binary};Copy the code
Here is the second method:
function doIncrementalTest(INPUT) {// This assumes that the DOM reference to the file selection box is passed directlyif (running) {
return; } // We need the File slice() method, Here are compatible writing var blobSlice = File. The prototype. Slice | | File. The prototype. MozSlice | | File. The prototype. WebkitSlice, File = input.files[0], chunkSize = 2097152, // chunks = math.ceil (file.size/chunkSize), CurrentChunk = 0, spark = new SparkMD5(), // Create SparkMD5 instance time, fileReader = new fileReader (); fileReader.onload =function (e) {
console("Read chunk number (currentChunk + 1) of chunks ");
spark.appendBinary(e.target.result); // append array buffer
currentChunk += 1;
if (currentChunk < chunks) {
loadNext();
} else {
running = false;
console.log("Finished loading!");
returnspark.end(); // Complete the calculation, return the result}}; fileReader.onerror =function () {
running = false;
console.log("something went wrong");
};
function loadNext() {
var start = currentChunk * chunkSize,
end = start + chunkSize >= file.size ? file.size : start + chunkSize;
fileReader.readAsBinaryString(blobSlice.call(file, start, end));
}
running = true;
loadNext();
}
Copy the code
You then simply call the doNormalTest or doIncrementalTest method in the Input onchange event handler and pass in the DOM node of the INPUT element
In addition, the author also has a detailed example of use in his demo. If you don’t understand, you can download the demo directly through the following download method
Download:
Finally, if you need this tool you can install it directly through NPM,
npm i spark-md5
Or download directly from the author’s Github:
Github.com/satazor/js-…