Accepting File Uploads : FileUpload « ASP.net Controls « ASP.NET Tutorial






Important properties, events

Enabled:      disable the FileUpload control.

FileBytes:    get the uploaded file as a byte array.

FileContent:  get the uploaded file as a stream.

FileName:     get the name of the file uploaded.

HasFile:      Returns True when a file has been uploaded.

PostedFile:   get the uploaded file wrapped in the HttpPostedFile object.

Focus:        set the form focus to the FileUpload control.

SaveAs:       save the uploaded file to the file system.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (upImage.HasFile)
        {
            if (CheckFileType(upImage.FileName))
            {
                String filePath = "~/UploadImages/" + upImage.FileName;
                upImage.SaveAs(MapPath(filePath));
            }
        }
    }

    bool CheckFileType(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".png":
                return true;
            case ".jpg":
                return true;
            default:
                return false;
        }
    }

    void Page_PreRender()
    {
        string upFolder = MapPath("~/UploadImages/");
        DirectoryInfo dir = new DirectoryInfo(upFolder);
        dlstImages.DataSource = dir.GetFiles();
        dlstImages.DataBind();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>FileUpload File</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <asp:Label
        id="lblImageFile"
        Text="Image File:"
        AssociatedControlID="upImage"
        Runat="server" />

    <asp:FileUpload
        id="upImage"
        Runat="server" />

    <br /><br />

    <asp:Button
        id="btnAdd"
        Text="Add Image"
        OnClick="btnAdd_Click"
        Runat="server" />

    <hr />

    <asp:DataList
        id="dlstImages"
        RepeatColumns="3"
        runat="server">
        <ItemTemplate>
        <asp:Image ID="Image1"
            ImageUrl='<%# Eval("Name", "~/UploadImages/{0}") %>'
            style="width:200px"
            Runat="server" />
        <br />
        <%# Eval("Name") %>
        </ItemTemplate>
    </asp:DataList>

    </div>
    </form>
</body>
</html>








3.19.FileUpload
3.19.1.Accepting File Uploads
3.19.2.FileUpload Test
3.19.3.Upload file to server
3.19.4.Uploading files using the new FileUpload control (C#)
3.19.5.Uploading files using the new FileUpload control (VB)
3.19.6.Changing the file-size limitation setting in the web.config file
3.19.7.The user selects a file from the local disk and the page manages to persist it to a server location