FileUpload Test : FileUpload « ASP.net Controls « ASP.NET Tutorial






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

<!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>FileUpload Control Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Choose a file to upload to the server<br />
    <asp:FileUpload ID="fupTest" runat="server" Width="400px"/>
    <br />
    <asp:Button ID="btnUpload" runat="server" Text="Upload File" OnClick="btnUpload_Click" />
    <asp:Label ID="labMessage" runat="server"></asp:Label>    
    <asp:Label ID="labInfo" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs

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 FileUploadTest : System.Web.UI.Page
{
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            if (fupTest.HasFile)
            {
                string path = @"C:\temp\";
                string fullname = path + fupTest.FileName;
                if (System.IO.File.Exists(fullname))
                {
                    labMessage.Text = "File already exists - uploaded cancelled";
                }
                else
                {
                    fupTest.SaveAs(fullname);
                    labMessage.Text = "File successfully uploaded";

                    int contentLength = fupTest.PostedFile.ContentLength;
                    string contentType = fupTest.PostedFile.ContentType;
                    labInfo.Text = "Content Type = " + contentType;
                    labInfo.Text += "<br/>";
                    labInfo.Text += " Content Length = " + contentLength;

                    byte[] input = new byte[contentLength];
                    input = fupTest.FileBytes;

                    System.IO.Stream myStream = fupTest.FileContent;
                    int index = 0;
                    while (index < myStream.Length)
                    {
                        byte aByte = (byte)myStream.ReadByte();
                        index++;
                    }

                }
            }
            else
            {
                labMessage.Text = "File was not specified";
            }
        }
        catch
        {
            labMessage.Text = "File was not uploaded";
        }
    }
}








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