ASP.NET - Check Whether the image Already exists in folder while uploading

Asked By sudheer on 02-Dec-10 07:56 AM
 
Hi,iam a fresher in .net,i have given a task i.e, to upload images.Ihad did it but the problem is when we upload a image with the name abc.jpg and if we again upload the same image with same name it has to alert that the image already exists.so it should not replace or rename it simply has to show the message that the image already exists.Can any one give me the code how to do this.

            Thank u..
Danasegarane Arunachalam replied to sudheer on 02-Dec-10 08:09 AM
Private Function CheckFileExist() As Boolean
      'Check file exist
      If System.IO.File.Exists(Server.MapPath("Filename with path")) = True Then
        'Alert File exist
      End If
    End Function
Reena Jain replied to sudheer on 02-Dec-10 08:09 AM
hi,

write this code

if (System.IO.File.Exists(SourceFileName))
{

}


this command will check whether file is exists or not

hope this will help you
undhad ashwin replied to sudheer on 02-Dec-10 08:24 AM
Hi Sudheer


First u copy paste below code inside page source
<head runat="server">
    <title>File Upload Controls File Exists Or Not</title>
</head>
<body>
    <asp:FileUpload ID="FileUpload1" runat="server" />

    <asp:Button ID="Button2" runat="server" Text="Button" />

    </form>
</body>
</html>

then after copy paste below code inside code behind page
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
      If FileUpload1.HasFile Then
        If System.IO.File.Exists(Server.MapPath("~") & FileUpload1.FileName) = True Then
          ScriptManager.RegisterStartupScript(Me, Me.GetType, "alert", "alert('image Already exists in folder');", True)
        Else

          FileUpload1.SaveAs(Server.MapPath("~") & FileUpload1.FileName)
        End If

      Else
      End If
    End Sub
if any query then reply
sudheer replied to undhad ashwin on 02-Dec-10 10:03 AM
Hi Aswin thank u for helping me.I tried your code but it getting some errors.Actually my code for uploading an image is .So how shall i write the code for checking image is exists.Please help me....


if (pimage.HasFile)

{

try

{

if (pimage.PostedFile.ContentLength < 102400)

{

string filename = pimage.FileName;

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

Label1.Text = "~//Images//" + filename;  //This label is used at another part of code in another button...

upload1.Text = "uploaded";

}

else

{

Response.Write("File has to be less than 1 MB");

}


}

catch (Exception ex)

{

upload1.Text = ex.Message.ToString();

}

}

else

{

upload1.Text = "You Had Not Specified a File";

}

sudheer replied to Reena Jain on 02-Dec-10 10:14 AM
Hi Thanks,
 I tried ur logic but i cant get it can u tell me by seeing my code where to implement it.

if (pimage.HasFile)

{

try

{

if (pimage.PostedFile.ContentLength < 20480)

{

string filename = pimage.FileName;

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

upload1.Text = "uploaded";
}

else

{

Response.Write("File has to be less than 1 MB");

}

}

catch (Exception ex)

{

upload1.Text = ex.Message.ToString();

}

}

else

{

upload1.Text = "You Had Not Specified a File";

}

Anoop S replied to sudheer on 02-Dec-10 01:47 PM
ou can find HttpPostedFile object in FileUpload control.

Such as,
HttpPostedFile MyFile;
int FileLen;
System.IO.Stream MyStream;

MyFileCollection = Request.Files;
MyFile = MyFileCollection[0];

FileLen = MyFile.ContentLength;

Now, you can check file length and display some messages to alert user.
Reena Jain replied to sudheer on 02-Dec-10 02:05 PM
hi,

here is the modified code for you

if (System.IO.File.Exists(pimage))
{

try

{

if (pimage.PostedFile.ContentLength < 20480)

{

string filename = pimage.FileName;

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

upload1.Text = "uploaded";
}

else

{

Response.Write("File has to be less than 1 MB");

}

}

catch (Exception ex)

{

upload1.Text = ex.Message.ToString();

}

}

else

{

upload1.Text = "You Had Not Specified a File";

}

hope this will help you

undhad ashwin replied to sudheer on 02-Dec-10 05:35 PM

if (pimage.HasFile)

{

try

{

if (pimage.PostedFile.ContentLength < 102400)

{

string filename = pimage.FileName;

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

Label1.Text = "~//Images//" + filename;  //This label is used at another part of code in another button...

upload1.Text = "uploaded";

}

else

{

Response.Write("File has to be less than 1 MB");

}


}

catch (Exception ex)

{

upload1.Text = ex.Message.ToString();

}

}

else

{

upload1.Text = "You Had Not Specified a File";

}

try above code