Jump to content

ASP.NET - Duplicate image upload to SQL Server database

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
engr

engr

    Newbie

  • Members
  • PipPip
  • 22 posts
Hi All,
I am uploading images to SQL Server 2005 using asp.net. The database table has a binary field, image length field,file path field and a field for the file type.

The issue is that the user is able to upload the same image to the database using different file names.

What unique property should I check for to prevent duplicate images from being uploaded. I tried checking the file length but two different images can have the same length, so this prevented a new image from being uploaded.

Anyone has any idea?

Thanks in advance

Edited by engr, 03 June 2009 - 01:48 AM.
Indicative Subject


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,822 posts
There isn't going to be an efficient way to do this. Worse, the user uploading the duplicate image may not know it's a duplicate under a different file name and could be irritated at being blocked.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
engr

engr

    Newbie

  • Members
  • PipPip
  • 22 posts
So tell me the inefficient way :)

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,822 posts
Open each binary field and compare it with the one to be saved :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
engr

engr

    Newbie

  • Members
  • PipPip
  • 22 posts
if i run this query on the database table -
select hashbytes('md5',cast(picture as varbinary(max))) from tblPicture

it gives the error of 'String or binary data would be truncated'.

I was thinking of comparing the input image against each image in the database by hashing it.

Any solutions???

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,822 posts
When you save an image, you could have a hash field and a length field. That should give you a pretty good indicator. Then you can do a "SELECT COUNT(*) FROM tblPicture WHERE HASH='value' AND LENGTH=length". IF the count is 0, go ahead and insert.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,297 posts
sounds in my ears as the probably best option, WP
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
engr

engr

    Newbie

  • Members
  • PipPip
  • 22 posts
But as I said hashing the image field gives an error of 'Binary field would be truncated'.

Is there another way of hashing?

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,822 posts
It sounds like you don't have a wide enough field to store the hash? Where exactly are you getting the error?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog