Valamas
Valamas

Reputation: 24729

How to identify files uploaded already if it is renamed

Is there a way to know if a user is uploading a files which already has been uploaded before.

This is without comparing file names. This is in case the user renames the file.

Scenario

Upvotes: 1

Views: 105

Answers (1)

Steve
Steve

Reputation: 216293

You could do a checksum on the file first submitted, store this checksum in a datatable with the filename. When the user submits again the renamed file you calculate again the checksum and search in the database if the checksum is already present.

The weakness of this solution is in the uniqueness of the checksum.
With this example I think you have good chances to get an unique checksum
(Expecting to be disowned)

public string GetChecksum(string filePath, HashAlgorithm algorithm)
{
    using (var stream = new BufferedStream(File.OpenRead(filePath), 100000))
    {
        HashAlgorithm SHA512 = new SHA512Managed();
        byte[] hash = SHA512.ComputeHash(stream);
        return BitConverter.ToString(hash).Replace("-", String.Empty);
    }
}

Upvotes: 2

Related Questions