1

I am using a form to upload multiple files. I have the following:

<input id="files" name="files[]" type="file" value="Add files..." multiple/>
<input id="addfiles" value="Add" type="button"/>

and

$("#addfiles").click(function() {
    f = $("#files").prop("files");
    for (index = 0; index < f.length; ++index) {
        if (!is_in_queue(f[index]))
        {
            queue.push(f[index]);
[...]

How can I compare two File objects? I need this comparison for is_in_queue. Unfortunately I see only the name attribute is set, but not the path (it's empty). That means a name-based comparison would fail for files with identical names in different paths.

Update: ideally I'd like this to be done client-side. The user needs to set some parameters for each uploaded file before uploading, and I'd like to avoid duplicating this workload.

Dan Nestor
  • 2,441
  • 1
  • 24
  • 45
  • Perhaps keep a hash or a checksum of the contents and match it up with that? [How to calculate md5 hash of a file using javascript](http://stackoverflow.com/questions/768268/how-to-calculate-md5-hash-of-a-file-using-javascript) – h2ooooooo Jan 27 '14 at 20:34
  • That's an idea. In theory though, it's possible that two different files have an identical content (although unlikely). – Dan Nestor Jan 27 '14 at 20:36
  • Perhaps keep both name AND checksum, and just hope that `file.txt` isn't present two places at once? Then again - if they DO have an identical content, isn't that what you want to avoid? :) – h2ooooooo Jan 27 '14 at 20:36
  • No, it's not. I am trying to avoid adding the same file to the queue twice. – Dan Nestor Jan 27 '14 at 20:37

1 Answers1

0

I think the best way for doing this is to upload all the files the user wants to upload at the same time. Of course checking for doubles should be done server side on the backend logic of your app.

Here, a link to get you started: http://www.html5rocks.com/en/tutorials/file/dndfiles/

EDIT: As @ikaros45 said, the best idea would be to check the MD5 hash of your file. But these are kept on the server side in a DB, so you need to request them or have a handler to check if a MD5 hash of the file to be uploaded is already present. Here is another link on the problem: How to calculate md5 hash of a file using javascript That's what you can do for client side. Because JavaScript is very limited it is hard to do file related actions inside the browser. Also by using the FileAPI you restrict your web app only to the newest browser versions.

Community
  • 1
  • 1
AlexandruB
  • 678
  • 3
  • 15
  • Ideally I'd like to do this client-side. The user must set some parameters for each file he's uploading, and I'd like to avoid duplicating the workload. – Dan Nestor Jan 27 '14 at 20:39
  • I think that is the right approach. Push everything to the server and check it there. Otherwise you have to calculate hashes (md5 for instance) in the server, send to the client, and calculate hashes on the client side with JS (which I don't know if it is possible). – bgusach Jan 27 '14 at 20:42
  • Unfortunately hashes are of no help (see comments to the question). – Dan Nestor Jan 28 '14 at 09:13
  • Add also the size of the file. The chance of two files having the same hash and size is infinitesimally low. Dropbox uses a hasing mechanism also to detect data duplication. That's how they will warn you uploading a song with a copyright with a different name than the original. – AlexandruB Feb 19 '14 at 21:15