Displayig image on page after Uploading

Sandhyab
Posted by Sandhyab under ASP.NET category on | Points: 40 | Views : 977
<%@ Page Language="C#" AutoEventWireup="true" Theme="FileUpload Theme" CodeFile="Question1_MultipleFileUpload.aspx.cs"
Inherits="Default2" %>

<%@ Register Src="~/UserControl/Menu.ascx" TagPrefix="uc1" TagName="Menu" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:Menu runat="server" ID="Menu1" />
<h2 class="Upload">
ASP.NET FileUpload example</h2>
<asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="SeaGreen">
</asp:Label>
<asp:Label ID="Label3" runat="server" Font-Size="Large" ForeColor="Red">
</asp:Label>
<br />
<br />
<asp:Label ID="Label2" runat="server" CssClass="Upload" ForeColor="Green" Text="Choose a file for upload."
Font-Bold="true">
</asp:Label>
<br />
<div class="Upload">
<asp:FileUpload ID="FileUpload1" runat="server" BackColor="Green" ForeColor="AliceBlue" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="FileUpload1"
Text="Please Select File To Upload" ForeColor="Red" Display="Dynamic" ErrorMessage="Enter Ur FirstName"></asp:RequiredFieldValidator>
<br />
<asp:FileUpload ID="FileUpload2" runat="server" BackColor="Green" ForeColor="AliceBlue" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FileUpload2"
Text="Please Select File To Upload" ForeColor="Red" Display="Dynamic" ErrorMessage="Enter Ur FirstName"></asp:RequiredFieldValidator>
<br />
<asp:FileUpload ID="FileUpload3" runat="server" BackColor="Green" ForeColor="AliceBlue" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="FileUpload3"
Text="Please Select File To Upload" ForeColor="Red" Display="Dynamic" ErrorMessage="Enter Ur FirstName"></asp:RequiredFieldValidator>
<br />
<asp:FileUpload ID="FileUpload4" runat="server" BackColor="Green" ForeColor="AliceBlue" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="FileUpload4"
Text="Please Select File To Upload" ForeColor="Red" Display="Dynamic" ErrorMessage="Enter Ur FirstName"></asp:RequiredFieldValidator>
<br />
<asp:FileUpload ID="FileUpload5" runat="server" BackColor="Green" ForeColor="AliceBlue" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="FileUpload5"
Text="Please Select File To Upload" ForeColor="Red" Display="Dynamic" ErrorMessage="Enter Ur FirstName"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button5" runat="server" Font-Bold="true" ForeColor="Green" OnClick="Button1_Click"
Text="Upload Now" />
<br />
</div>
<br />
<asp:Image ID="Image1" runat="server" />
<asp:Image ID="Image2" runat="server" />
<asp:Image ID="Image3" runat="server" />
<asp:Image ID="Image4" runat="server" />
<asp:Image ID="Image5" runat="server" />
</div>
</form>
</body>
</html>


Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{

HttpFileCollection uploadFilCol = Request.Files;
for (int i = 0; i < uploadFilCol.Count; i++)
{
HttpPostedFile file = uploadFilCol[i];
string fileExt = Path.GetExtension(file.FileName).ToLower();
string fileName = Path.GetFileName(file.FileName);
if (fileName != string.Empty)
{
if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" || fileExt == ".jpeg" || fileExt == ".png")
{
file.SaveAs(Server.MapPath("~/Images/") + fileName);
if (i == 0)
{
Image1.ImageUrl = "~/Images/" + fileName;
}
if (i == 1)
{
Image2.ImageUrl = "~/Images/" + fileName;
}
if (i == 2)
{
Image3.ImageUrl = "~/Images/" + fileName;
}
if (i == 3)
{
Image4.ImageUrl = "~/Images/" + fileName;
}
if (i == 4)
{
Image5.ImageUrl = "~/Images/" + fileName;
}

}
else
{
Label3.Text = "File extension not permitted: " + fileExt;
}

}
else
{
Label3.Text = "Please Selece a file to Upload";
}

}
}
catch (Exception ex)
{
throw ex;
}
}

}


Make sure that You have Added A folder Images in Your Application To save Uplaoded Images.

Comments or Responses

Login to post response