tags:

views:

2225

answers:

9

Is there any way to calculate the md5 hash of a file before the upload to the server using javascript?

A: 

Take a look at this.

Pablo Santa Cruz
That's just simple md5 hashing of a string, I wanted to know if that's possible with a file. Thanks
LuRsT
File!? With JavaScript!?Forget about it.
Pablo Santa Cruz
+1  A: 

I don't think this is possible because JavaScript has limited filesystem access.

Daniel A. White
+5  A: 

While there are JS implementations of the MD5 algorithm, it is not possible for javascript to read files on the filesystem.

Paul Dixon
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
It will be possible in FF3.6 ;)
powtac
that's a pretty broad statement since the browser wasn't specified at all.
Luca Matteis
+2  A: 

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.

bendewey
+2  A: 

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

James Black
A: 

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.

kbosak
A: 

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.

pluckyglen
A: 

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.

JohnnieWalker
A: 

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.

Marco