Wednesday, December 10, 2008

Upload a file using asp.net 2.0 c# and file control sample code

<%

@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!

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>Untitled Page</title>

</

head>

<

body>

<form id="form1" runat="server">

<div>

<asp:FileUpLoad id="FileUpLoad1" runat="server" />

<

asp:Button id="UploadBtn" Text="Upload File" OnClick="UploadBtn_Click" runat="server" Width="105px" />

&nbsp;&nbsp;<asp:Label ID="Label1" runat="server" Width="188px"></asp:Label></div>

</form>

</

body>
</
html>
 
 
code behind
 

using

System;

using

System.Data;

using

System.Configuration;

using

System.Collections;

using

System.Web;

using

System.Web.Security;

using

System.Web.UI;

using

System.Web.UI.WebControls;

using

System.Web.UI.WebControls.WebParts;

using

System.Web.UI.HtmlControls;

public

partial class Default5 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void UploadBtn_Click(object sender, EventArgs e)

{

if (FileUpLoad1.HasFile)

{

FileUpLoad1.SaveAs(

@"C:\webfiles\" + FileUpLoad1.FileName);

Label1.Text =

"File Uploaded: " + FileUpLoad1.FileName ;

}

else

{

Label1.Text =

"No File Uploaded.";

}

}

}

No comments: