Often we need unique naming of image files when we upload it from webpage.We can not keep same name as what it was originally.It may overwrite any old file in case we already have a file with same name.
Popular convention of unique naming image with primary key
Often developers use file name as primary key of a table in database concatenated with image file extension. Suppose we have a table in the database named ‘tbl_cars’. Say there is a row CarID is primary key and for some row the primary key is 1001, so we can progrmatically name the image of that car as 1001.JPG.
Now a days , you will see very big unique name of images uploaded by visitors. In asp.net we can do this by using GUID. Code will look like this-
Guid UniqueFilename; UniqueFilename=Guid.NewGuid(); // u get a uninque alpha numeric ID , StringBuilder imageFilename= new StringBuilder(UniqueFilename.toString()).Append(".JPG").ToString()
Caching problem of naming image with primary key
I was developing a web services for a mobile application. Banner of products were named using primary key.To improve the performance, in mobile app banner images were cached . Now it works fine until we change the banner.But when change the banner image it does not reflect immediately in the mobile apps .The reason behind this is-even if we change the image , the image file name remains same. This issue can be resolved by naming images with GUID and every time we update image , save it with new file name.