Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews Dot Net Technologies Interviews:.Net Architecture.Net Database.Net Deployment.NET web servicesADO.NETADO.NET 2.0ASP ProgrammingASP.NetASP.NET 2.0ASP.NET CachingASP.Net MVCBizTalkC# (Sharp) Programming LanguageCOM+Crystal ReportsDataGrid (Grid view)Dot NetDot Net AssemblyDot Net Code SecurityDot NET crystal reportsDot Net FrameworkDot Net RemotingDot Net WindowsFormsEntity FrameworkMicrosoft .Net MobileMicrosoft BasicsMicrosoft.NETMicrosoft.NET 2.0Mixed MicrosoftMono FrameworkMOSSMSFMTSReporting ServicesSenior .Net DeveloperVB .NetVB .Net DeveloperVB.NET FrameworkWCF (Windows Communication Foundation)WCF Data ServicesWeb Forms
Copyright © 2018. All Rights Reserved
Web Forms Interview Question:
How to upload an image files only in .net web forms?
Submitted by: AdministratorSee 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>");
}
}
}
Submitted by: Administrator
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>");
}
}
}
Submitted by: Administrator
Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.