In Asp.net How to upload Image In to server
In Asp.net How to upload Image In to server? Is a major task, in this Post we learn this.
File upload control in asp.net programming is a compilation of two controls of
asp.net. One is a Text Box and second is a button control. Basically this
control gives the facility to save the image in to the server by user. In programming
Many time user want to use this control for ‘Inserting Image of user, Uploading
Resume (CV), Uploading PDF file to server, and many kind of work.
Code : for : In Asp.net How to upload Image In to server Aspx page :-
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="CS.aspx.cs"
Inherits="CS"
%>
<!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">
<asp:FileUpload ID="FileUpload1"
runat="server"
/>
<asp:Button ID="btnUpload"
runat="server"
Text="Upload"
OnClick="Upload"
/>
<hr />
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="false"
ShowHeader="false">
<Columns>
<asp:BoundField DataField="Text"
/>
<asp:ImageField DataImageUrlField="Value" ControlStyle-Height="100" ControlStyle-Width="100" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
Code for : In Asp.net How to upload Image In to server aspx.cs page
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
using System.IO;
using
System.Collections.Generic;
public partial class
CS : System.Web.UI.Page
{
protected void Page_Load(object sender,
EventArgs e)
{
if (!IsPostBack)
{
string[] filePaths =
Directory.GetFiles(Server.MapPath("~/Images/"));
List<ListItem> files = new
List<ListItem>();
foreach (string filePath in
filePaths)
{
string fileName =
Path.GetFileName(filePath);
files.Add(new
ListItem(fileName, "~/Images/" + fileName));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
}
protected void Upload(object sender,
EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName =
Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") +
fileName);
Response.Redirect(Request.Url.AbsoluteUri);
}
}
}
Asp.net Related Post:
- Limit Number of Characters in a TextArea using jQuery
- Get selected radio button values using JQuery
- How do you do html text encodes using JavaScript
- Example jQuery Validate on CheckBoxList using C#
- How do you do html text encodes using JavaScript
- Check Uncheck all html CheckBox controls using jQuery:
- Check Uncheck all asp.net CheckBox in asp.net using jQuery
- Example of jQuery Validate on Radiobuttonlist in Asp.Net using C#
- Validate ASP.Net RadioButtonList using JavaScript Example
- Example of jQuery Validate on Radiobuttonlist in Asp.Net using C#
- Cropping image using jQuery in asp.net
- Displaying the textbox value in javascript Messagebox
- Get selected radio button values using JQuery
- fill data into Dropdown list by using Jquery
- jQuery Crop Image in Asp.net using Jcrop jQuery
- Example jQuery Validate on CheckBoxList using C#
- Check Uncheck all asp.net CheckBox in asp.net using jQuery
- Check Uncheck all html CheckBox controls using jQuery:
- Asp.net CheckBoxList using jQuery.
- Get selected radio button values using JQuery.
Related Questions: