image name already exists,image name already check and image size check in asp.net :
aspx :
<div>
<asp:Label ID="lblUploadImage" runat="server"
Text="Image Upload:" ></asp:Label>
<asp:FileUpload ID="imgUpload" runat="server" />
<asp:Button ID="btnSave" runat="server"
Text="save" onclick="btnSave_Click" />
<asp:Label ID="lblError" runat="server" Text="">
</asp:Label>
<asp:Label ID="lblError" runat="server" Text="">
</asp:Label>
</div>
aspx:cs :
protected void btnSave_Click(object sender, EventArgs e)
{
string f_name,Extension,ServerPath ,imgFileName;
if (imgUpload.HasFile == true)
{
Extension = Path.GetExtension(imgUpload.PostedFile.FileName);
//Image Format Check :
if (Extension == ".JPG" || Extension == ".GIF" || Extension == ".jpg"
|| Extension == ".gif" || Extension == ".png")
{
//Image Size Check :
if (imgUpload.PostedFile.ContentLength < 102400) // -- 100KB
{
f_name= Path.GetFileName(imgUpload.FileName);
ServerPath = Server.MapPath(".");
imgFileName= path + "\\Image\\" + f_name;
//Image Name already Exists Check :
if (System.IO.File.Exists(imgFileName))
{
lblError.Text = "Image Name Already Use";
lblError.ForeColor = System.Drawing.Color.Red;
}
else
{
//Image Upload :
imgUpload.SaveAs(imgFileName);
lblError.Text="Image Uploaded Sucessfully !!!";
}
}
else
{
lblError.Text = "Image Size allow only Max 100 KB";
lblError.ForeColor = System.Drawing.Color.Red;
}
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "Success","<script type='text/javascript'>alert('You must upload on image file with
one of following Extensions::')
</script>");
}
}
}
------------------------------------------------------------
--- Image .jpg,.gif,.png only Allow .doc or xlsheet Not allowed :
No comments:
Post a Comment