Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing File objects in JavaScript

I have a drop zone for files in a JS application. I want to filter out duplicate files being dropped into the application, but I can't seem to find a proper way to compare two File objects pointing to the same file. All I could find is comparison by "name + file size + modified date" combo, but it's not 100% proof, since the path is not revealed in the name attribute.

Is there a way to do it that I just missed?

like image 964
Igor K. Avatar asked Apr 10 '14 14:04

Igor K.


1 Answers

I know that the answer comes too late, but I was confronting with the same issue and maybe others too.

I doubt that there is a better method than the one that you're using. Indeed it's not 100% proof because you can have two files with the same name, size and last modified date, but they can sit in two separate folders and be different.

Using equality (===) between File objects is also failing if you have the same file chosen by two different <input type="file">.

I think another way will be in addition to what you've done to use FileReader to actually compare the files by content. But this will be very expensive.

like image 184
Adrian Ber Avatar answered Sep 17 '22 08:09

Adrian Ber