Web Forms Question:
Download Job Interview Questions and Answers PDF
How to upload an image files only in .net web forms?
Answer:
See the code,
Fileupload.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Fileupload.aspx.cs" Inherits="FileuploadDemo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Upload Image Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</div>
</form>
</body>
</html>
Fileupload.aspx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class FileuploadDemo : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
string fileFullname = this.FileUpload1.PostedFile.FileName;
string dataName = DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss");
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
if (FileUpload1.PostedFile.ContentType.ToUpper().IndexOf("IMAGE") > -1)
{
System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
int Width = img.Width;
int Height = img.Height;
if (Width > 1000 || Height > 1000 || FileUpload1.PostedFile.ContentLength > 1024 * 1024 * 200)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "Startup",
"<script language='javascript'>alert('The image size is too large!');</script>");
}
else
{
if (type == "jpg" || type == "gif" || type == "bmp" || type == "JPG" || type == "GIF")
{
string ImagePath = "images/";
string sPath = Server.MapPath(ImagePath) + dataName + fileName;
string imgPath = ImagePath + dataName + fileName;
this.FileUpload1.PostedFile.SaveAs(sPath);
this.ClientScript.RegisterStartupScript(this.GetType(),
"Startup", "<script language='javascript'>alert('Success!');</script>");
this.Image1.ImageUrl = imgPath;
this.btnSubmit.Enabled = false;
this.btnSubmit.Text = "Success!";
this.btnSubmit.Enabled = true;
}
else
{
this.ClientScript.RegisterStartupScript(this.GetType(), "Startup",
"<script language='javascript'>alert('File type is not right!');</script>");
}
}
}
else
{
this.ClientScript.RegisterStartupScript(this.GetType(), "Startup",
"<script language='javascript'>alert('The uploaded file is not a image file!');</script>");
}
}
}
Fileupload.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Fileupload.aspx.cs" Inherits="FileuploadDemo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Upload Image Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</div>
</form>
</body>
</html>
Fileupload.aspx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class FileuploadDemo : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
string fileFullname = this.FileUpload1.PostedFile.FileName;
string dataName = DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss");
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
if (FileUpload1.PostedFile.ContentType.ToUpper().IndexOf("IMAGE") > -1)
{
System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
int Width = img.Width;
int Height = img.Height;
if (Width > 1000 || Height > 1000 || FileUpload1.PostedFile.ContentLength > 1024 * 1024 * 200)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "Startup",
"<script language='javascript'>alert('The image size is too large!');</script>");
}
else
{
if (type == "jpg" || type == "gif" || type == "bmp" || type == "JPG" || type == "GIF")
{
string ImagePath = "images/";
string sPath = Server.MapPath(ImagePath) + dataName + fileName;
string imgPath = ImagePath + dataName + fileName;
this.FileUpload1.PostedFile.SaveAs(sPath);
this.ClientScript.RegisterStartupScript(this.GetType(),
"Startup", "<script language='javascript'>alert('Success!');</script>");
this.Image1.ImageUrl = imgPath;
this.btnSubmit.Enabled = false;
this.btnSubmit.Text = "Success!";
this.btnSubmit.Enabled = true;
}
else
{
this.ClientScript.RegisterStartupScript(this.GetType(), "Startup",
"<script language='javascript'>alert('File type is not right!');</script>");
}
}
}
else
{
this.ClientScript.RegisterStartupScript(this.GetType(), "Startup",
"<script language='javascript'>alert('The uploaded file is not a image file!');</script>");
}
}
}
Download Microsoft Web Forms Interview Questions And Answers
PDF
Previous Question | Next Question |
How to upload a file on web forms? | How to select multiple non-sequential dates at Code-Behind in Microsoft web forms? |