Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am uploading files and I want to calculate md5 for each file

I have written the function calcMD5 that is working well. THE PROBLEM is that when I choose more than one file, it first finish uploading and then it startcalculating the md5. I also have debugged it but it does not even enter the function till it send all the blolbs to the server and upload everything, then it starts calculating

Can anybody help me with this issue

this is my code:


C#
$(document).ready(function () {
    $("#btnUpload").click(function (evt) {
        var fl = document.getElementById("files");
        var L = fl.files.length;
        for (var i = 0; i < L ; i++) {
            var file = fl.files[i];
            var file = fl.files[i];
    calcMD5(file);
    var bytes_per_chunk = 1024 * 1024; //1048576
    var start = 0;
    var end = bytes_per_chunk;
    var size = file.size;
    var j = bytes_per_chunk;
    while (start < size) {
        //push the fragments to an array
        blobs.push(file.slice(start, end));
        start = j;
        j = start + bytes_per_chunk;;
        end = start + bytes_per_chunk;
        if (end > size) {
            end = size;
        }
    }
    var fileName = file.name;
    var fileType = file.type;
    var fileUp = (100 * bytes_per_chunk) / file.size;
    var rec = 0;
    var count = 0;
    var temp = 0;
    while (blob = blobs.shift()) {
        rec = fileUp + rec;
        if (rec > 100) {
            rec = 100;
        }
        xhr.open('POST', 'FileUploadHandler.ashx', false);
        xhr.setRequestHeader('X_FILE_NAME', fileName);
        xhr.setRequestHeader('Content-Type', fileType);
        xhr.send(blob);
        count++;
        }
    });
});

function calcMD5(f) {
    // MD5 Calculation
    var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
    file = f,
    chunkSize = 1024 * 1024,                             // Read in chunks of 2MB
    chunks = Math.ceil(file.size / chunkSize),
    currentChunk = 0,
    spark = new SparkMD5.ArrayBuffer(),
    fileReader = new FileReader();

    fileReader.onload = function (e) {
        console.log('read chunk nr', currentChunk + 1, 'of', chunks);
        spark.append(e.target.result);                   // Append array buffer
        currentChunk++;

        if (currentChunk < chunks) {
            loadNext();
        } else {
            console.log('finished loading');
            console.info('computed hash', spark.end());  // Compute hash
        }
    };

    fileReader.onerror = function () {
        console.warn('oops, something went wrong.');
    };
    function loadNext() {
        var start = currentChunk * chunkSize,
            end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;

        fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
    }
    loadNext();
}
Posted
Comments
F-ES Sitecore 1-Sep-15 10:16am    
What you're doing has very little value, anyone can replace or remove the javascript on your page.
Lalyka 1-Sep-15 10:19am    
would you please recommend me what to do?
F-ES Sitecore 1-Sep-15 10:24am    
I'm not sure why you're calculating the hash on the client, you don't seem to be doing anything with the hash value.
Lalyka 1-Sep-15 10:26am    
I will, I need to pass it to server, but the problem is that at the moment it calculates the hash after the upload is finished. if it works I need to send the md5 value also to server to check the uplaod correctness
F-ES Sitecore 1-Sep-15 11:01am    
I don't understand what use that has...so the file they selected is the file they uploaded, I fail to see the value?

I already Solved my problem
the solution is to start slicing the file in
C#
fileReader.onload = function (e) {
    i++;
    console.log('read chunk nr', currentChunk + 1, 'of', chunks);
    spark.append(e.target.result);                   // Append array buffer
    currentChunk++;
    if (currentChunk < chunks) {
        loadNext();
    }else 
    {
         console.log('finished loading');
         console.info('computed hash', spark.end());  // Compute hash
         ret = spark.end();
         UploadFile(); // here in upload part we do the file slicing
    }
}



Thanks anyway :)
 
Share this answer
 
v2
Comments
hypermellow 4-Sep-15 4:58am    
What was the solution to your problem?

The code snippet you posted is identical to code you posted that you said was giving you problems?
Lalyka 4-Sep-15 5:13am    
I updated the answer
hypermellow 4-Sep-15 5:17am    
Looks better now, thanks for updating.
If you use a debugger, you can always stop at the point before the call to the function, not just inside this function. This way, you can see where the control goes instead of the call you need.

Also, your bug is probably lexical. JavaScript does not interpret code line by line from the very beginning; if first-run parsing and lexical analysis fails, even correct lines won't be executed; and the usual exception handling won't help you to detect it. This statement may sound surprising to you, but it's hard to check-up :-).

Look at the line #5, first loop in your code. The { bracket is not closed, it looks like. One method of detecting lexical bugs is described in this chapter of my article: JavaScript Calculator 5. Handling Lexical Errors.

By the way, the algorithm, as well as many others, is implemented in JavaScript here: https://code.google.com/p/crypto-js.

[EDIT]

See also my comment to the question. For cryptographic purposes, MD5 or SHA-1 are unreliable, as these algorithms are broken. For such purposes, one should rather use one from the SHA-2 family, or SHA-3:
https://en.wikipedia.org/wiki/Cryptographic_hash_function[^],
https://en.wikipedia.org/wiki/MD5[^],
https://en.wikipedia.org/wiki/SHA-1[^],
https://en.wikipedia.org/wiki/SHA-2[^],
https://en.wikipedia.org/wiki/SHA-3[^].

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900