joomla visitors
Mithila Software Blog | Creating unique Image Names for uploaded files in asp.net

Creating unique Image Names for uploaded files in asp.net

November 27, 2012 Posted by Mithila Software

While programming a multiuser application in asp.net , for example a social sharing site like - video sharing , photo sharing , social network or so..There is a provision where members can upload their images.

Let us explore popular approaches to prevent images name clash or preventing images from over writing when two members of your site upload image bearing same name and type.

 

Approach # 1

Saving Image names with GUID value

1.Images are saved with GUID name that is - people upload their images from their end having a name xx.jpg , and your program saves it as <generated guid>.jpg

The code for unique image name is generated like below

string uploaded_image_unique_name = string.Concat(Guid.NewGuid().ToString(),".jpg");

 

The benefit of this approach is that every time a 100% pure unique name is generated

Ex - 7f94c135-931c-49a1-b19b-f68fad85d586.jpg

 

However , the downside is that when we open the image folder when we are storing all these images we come across images files of the site members bearing this cryptic name.And we have no clue whose image it is until we open it.

Apart from that consider our images below again -

1.7f94c135-931c-49a1-b19b-f68fad85d586.jpg (Our Image)

2.HSJ_in_friends_party.jpg  (User's Originally uploaded image )

Clearly you can see that image at #2 is more meaningful and so when search engines are ranking the images especially Google Images  they place you better in rank for Image No 2 .

 

Approach # 2

Saving Image with original Image name but appended with GUID

Programmers Place uploaded images inside user's own directory coz username is unique.So it goes like this

1.HSJ creates an account on x.com with unique username HSJ

2.The website creates a folder for HSJ.

3.When HSJ uploads an image file to his profile for say hsj.jpg the program saves it as

hsj-<GUID>.jpg to HSJ folder.

This approach is also good but it can create thousands of directories on server if site becomes very popular.

 

Approach # 3

Saving Image after checking for duplicate one

 

In this approach we loop through the upload directory for any duplicate record and if found we append the loop no to that file.Check the example below -

 

const string ImageDirectory = "~/Uploads/";

string ImagePath = ImageDirectory + FileUpload1.FileName;

string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(FileUpload1.FileName);

int count = 1;

while (System.IO.File.Exists(Server.MapPath(ImagePath)))

{

ImagePath = string.Concat(ImageDirectory , fileNameWithoutExtension, "-", iteration, ".jpg"); count++;

}

FileUpload1.SaveAs(Server.MapPath(ImagePath));

 

 

1 First of all we declared the base directory where we are going to store images

2 We combine the path with our uploaded file name and also store our uploaded file name without file extension.
3 Now we loop through the directory for the existence of any already existing same name image file and if we found one we append our ImagePath to our uploaded file name with loop count where we saw the duplicate value.
4Finally we save this new unique image file on server.

Tags:

Asp.Net

blog comments powered by Disqus
Address ::
Mithila Software Solutions,
Green House , 3 GM Road ,
Near Radio Station , Darbhanga
PIN:: 846004
Phone :: # 099-397-49350