asp.net FileUpload: how to use FileUpload
FileUpload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUpload.aspx.cs" Inherits="FileUpload" %>
<!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>asp.net FileUpload: how to use FileUpload</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Green">FileUpload: how to upload file</h2>
<asp:Label ID="Label1" runat="server">
</asp:Label>
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_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 FileUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Font.Italic = true;
FileUpload1.BackColor = System.Drawing.Color.Green;
FileUpload1.ForeColor = System.Drawing.Color.AliceBlue;
Button1.ForeColor = System.Drawing.Color.Green;
Button1.Font.Bold = true;
}
}
protected void Button1_Click(object sender, EventArgs e) {
if (FileUpload1.HasFile)
try
{
FileUpload1.SaveAs("C:\\Upload\\" + FileUpload1.FileName);
Label1.Text = "Upload successfull. <br />" +
"File Name: " + FileUpload1.PostedFile.FileName +
"<br />File Type: " + FileUpload1.PostedFile.ContentType+
"<br />File Length: " + FileUpload1.PostedFile.ContentLength;
}
catch (Exception ex)
{
Label1.Text = "Error: " + ex.Message.ToString();
}
else
{
Label1.Text = "Select a file";
}
}
}
- asp.net GridView: how to use
- asp.net FormView: how to use
- asp.net DetailsView: how to use
- How to change Button control Style, ForeColor, BackColor programmatically in asp.net
- Populating ListBox with Array in asp.net
- DropDownList and Array example: populating a DropDownList with Array DataSource
- asp.net example: get the ConnectionStrings List programmatically
- HyperLink Control example: how to use HyperLink in asp.net
- Session Example: how to use Session in asp.net
- How to create and use User Control in asp.net