Is there any way to calculate the md5 hash of a file before the upload to the server using javascript?
I don't think this is possible because JavaScript has limited filesystem access.
While there are JS implementations of the MD5 algorithm, it is not possible for javascript to read files on the filesystem.
There is a couple scripts out there on the internet to create an MD5 Hash.
The one from webtoolkit is good, http://www.webtoolkit.info/javascript-md5.html
Although, I don't believe it will have access to the local filesystem as that access is limited.
You can't get access to the actual file that is being transferred from javascript, but if you wrote your own upload software, in something like Flash, or an applet, assuming permissions were set, you could then do the hash and send the data.
UPDATE:
If you use Firfox 3.6beta it appears there is a way to select files from the user's harddrive:
https://developer.mozilla.org/en/Using_files_from_web_applications
I don't believe there is a way in javascript to access the contents of a file upload. So you therefore cannot look at the file contents to generate an MD5 sum.
You can however send the file to the server, which can then send an MD5 sum back or send the file contents back .. but that's a lot of work and probably not worthwhile for your purposes.
Without a plugin you can't access that binary data. You should look into using a Flash-based upload tool. I have colleagues who have used SWFUpload, but I don't know how to get access to the file content itself. You might have to alter the SWF itself to allow this.
You could use the specially developed API (fully javascript compatible thanks to JSONP) http://www.filemd5.net/API which I believe solves exactly the problem you are asking as long the files are available on the web.
Apart from the impossibility to get file system access in JS, I would not put any trust at all in a client-generated checksum. So generating the checksum on the server is mandatory in any case. – Tomalak Apr 20 '09 at 14:05
Which is useless in most cases. You want the MD5 computed at client side, so that you can compare it with the code recomputed at server side and conclude the upload went wrong if they differ. I have needed to do that in applications working with large files of scientific data, where receiving uncorrupted files were key. My cases was simple, cause users had the MD5 already computed from their data analysis tools, so I just needed to ask it to them with a text field.