<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>
FileUpload Control
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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;
using System.IO;
public partial class Default_aspx : System.Web.UI.Page
{
protected void btnSave_Click(object sender, EventArgs e){
string str = "";
if (FileUpload1.HasFile) {
try {
str += "Uploading file: " + FileUpload1.FileName;
FileUpload1.SaveAs("c:\\" + FileUpload1.FileName);
str += "
Saved As: " + FileUpload1.PostedFile.FileName;
str += "
File Type: " + FileUpload1.PostedFile.ContentType;
str += "
File Length (bytes): " + FileUpload1.PostedFile.ContentLength;
str += "
PostedFile File Name: " + FileUpload1.PostedFile.FileName;
} catch (Exception ex) {
str += "
Error
Unable to save c:\\" + FileUpload1.FileName + "
" + ex.Message;
}
} else {
str = "No file uploaded.";
}
lblMessage.Text = str;
lblDisplay.Text = "";
}
protected void btnDisplay_Click(object sender, EventArgs e)
{
string str = "File: " + FileUpload1.FileName + "
";
if (FileUpload1.HasFile)
{
try
{
Stream stream = FileUpload1.FileContent;
StreamReader reader = new StreamReader(stream);
string strLine = "";
do
{
strLine = reader.ReadLine();
str += strLine;
} while (strLine != null);
}
catch (Exception ex)
{
str += "
Error
Unable to display " + FileUpload1.FileName + "
" + ex.Message;
}
}
else
{
str = "No file uploaded.";
}
lblDisplay.Text = str;
lblMessage.Text = "";
}
}