ASP.NET - query - Asked By on 31-Jan-12 12:18 AM

Upload actual  image in folder and check duplicate image.
Jitendra Faye replied to on 31-Jan-12 12:23 AM
To upload file you can use FileUpload control like this-

use FileUpload control to upload a file

<form id="form1" runat="server">

<asp:FileUpload id="FileUploadControl" runat="server" />

<asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" />

<br /><br />

<asp:Label runat="server" id="StatusLabel" text="Upload status: " />



In the button click write this code

protected void UploadButton_Click(object sender, EventArgs e)






string filename = Path.GetFileName(FileUploadControl.FileName);

FileUploadControl.SaveAs(Server.MapPath("~/") + filename);

StatusLabel.Text = "Upload status: File uploaded!";


catch(Exception ex)


StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;




Note - to check duplicate file check that file is exist or not in that folder using File.Exist() method.

Try and let me know.
Venkat K replied to on 31-Jan-12 12:28 AM

Here i am sharing the code which i already using.. In an high level you need to use File.Exists to check whether the file exists in the folder or not.

//First of all, clear data uploaded at previous time.

if (e.FirstUploaded)




//Save file and thumbnail

string physGalleryPath = Server.MapPath(galleryPath);

string sourceFileName = e.SourceFile.FileName.ToString();

//If file with the same name exist, it will not be saved

if (!System.IO.File.Exists(sourceFileName))



Path.Combine(physGalleryPath, sourceFileName));


private void InitGallery()


//Delete source files

DirectoryInfo dir = new DirectoryInfo(Server.MapPath(galleryPath));

foreach (FileInfo file in dir.GetFiles())




//Delete thumbnails

dir =

new DirectoryInfo(Server.MapPath(galleryPath) + "/Thumbnails");

foreach (FileInfo file in dir.GetFiles())




XmlDocument descriptions = new XmlDocument();



descriptions.Save(Server.MapPath(galleryPath +




D Company replied to on 31-Jan-12 12:37 AM
Hello friend.

You can check for a file's existence with the System.IO.File.Exists method. It takes a path string as its argument.

For manipulating the path string should a file exist, It's a great utility for doing just what you need

//pass the path of file
Use these lines of code to do this.

if (System.IO.File.Exists("Path")) 
    //restrict user here, display message like image already exist


//and to save the file


let me know if you need more assistence on this!!
Riley K replied to on 31-Jan-12 01:16 AM

First check if the file already exists in the server, if yes then only upload it

sample code

If System.IO.File.Exists(Server.MapPath(fileul.FileName)) Then
MsgBox("File is already uploaded")
End If