c# tutorials and examples code for web developers

Thursday, October 9, 2008

ASP.NET FileUpload Control Example

FileUpload Control Example

FileUpload control is a Standard ToolBox control. Here I show a simple use of FileUpload control.

Simple Use
First create a folder name UploadFolder in your computer C:\ Drive. Then create a Web Form name FileUpload.aspx. FileUpload.aspx page code is here.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e) {
        if (FileUpload1.HasFile)
                FileUpload1.SaveAs("C:\\UploadFolder\\" + FileUpload1.FileName);
                Label1.Text = "Uploaded File: " + FileUpload1.PostedFile.FileName +
                    "<br /> File Size: " + FileUpload1.PostedFile.ContentLength;
            catch (Exception ex)
                Label1.Text = "An Error occured: "+ ex.Message.ToString();
            Label1.Text = "First choose an file then click button";

<html xmlns="">
<head runat="server">
    <title>FileUpload Control Example</title>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Font-Size="X-Large" ForeColor="Crimson" ></asp:Label>
        <hr />
        <asp:FileUpload ID="FileUpload1" runat="server" BackColor="Chocolate" />
        <asp:Button ID="Button1" runat="server" Text="Upload the selected file" OnClick="Button1_Click" />

Related examples