ASP Net Controls ASP.Net Tutorial

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



    FileUpload Control Test


    
    

    Choose a file to upload to the server
    
    
    
        
    
    

    


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 += "
";
                    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";
        }
    }
}