Mega Code Archive

 
Categories / ASP.Net Tutorial / ASP Net Controls
 

FileUpload Test

<%@ 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";         }     } }