Verify whether File Exists when uploading using FileUpload control in ASP.Net

Last Reply on Mar 17, 2014 06:33 AM By Mudassar

Posted on Mar 17, 2014 06:31 AM

In my application Im saving multiple files(image,pdf,txt) to a folder.  Suppose I have an image with name "image1", if this image already exists in the folder and if another user tries to add another image with the same name "image1" then, it should give message to change the File name.

I had used below code:

 string filePath = DamRep_FileUpload.PostedFile.FileName;
 string filename = Path.GetFileName(filePath);
 string contenttype = String.Empty;

            if (contenttype != String.Empty)
            {
                 if (File.Exists("~/FTP/File/"))
                 {
                     
                     LMsg.Visible = true;
                     LMsg.Text = "File name already exist. Change the File name if u want to save this file";
                 }
                 else
                 {
                    
                     string fileName = Path.GetFileName(DamRep_FileUpload.PostedFile.FileName);
                     DamRep_FileUpload.PostedFile.SaveAs(Server.MapPath("~/FTP/File/") + fileName);
                     Response.Redirect(Request.Url.AbsoluteUri);
                 }
            }

but its not working.

Please suggest some help as per my requirement.

Posted on Mar 17, 2014 06:33 AM

You have not specified the File Name while checking

Replace

if (File.Exists("~/FTP/File/"))

with

if (File.Exists(Server.MapPath("~/FTP/File/") + filename)

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html