Large File Upload with C#
Posted by Sean RyanOct 10
I wrote this a few years ago and don’t know why I haven’t posted it, so for those interested in file upload with C#. pay attention to the lMaxFileSize var. I have no doubt this can be improved upon with 4.0
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="up.aspx.cs" Inherits="up.upload" %>
<!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>Upload Script</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout" BGCOLOR="lightgrey">
<form id="upForm" method="post" encType="multipart/form-data" runat="server">
<INPUT id="File1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 160px" type="file"
name="File1" runat="server">
<asp:button id="cmdUpload" style="Z-INDEX: 102; LEFT: 272px; POSITION: absolute; TOP: 160px"
runat="server" Text="Upload"></asp:button><asp:dropdownlist id="ddlPath" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 120px" runat="server"
AutoPostBack="True"></asp:dropdownlist><asp:label id="lblMessage" style="Z-INDEX: 104; LEFT: 24px; POSITION: absolute; TOP: 200px"
runat="server" Width="448px"></asp:label><asp:linkbutton id="LinkButton1" style="Z-INDEX: 105; LEFT: 16px; POSITION: absolute; TOP: 88px"
runat="server">Reset</asp:linkbutton>
<asp:Label id="Label1" style="Z-INDEX: 106; LEFT: 24px; POSITION: absolute; TOP: 16px" runat="server"
Font-Bold="True">Upload Utility</asp:Label>
<asp:Label id="lblPathMessage" style="Z-INDEX: 107; LEFT: 24px; POSITION: absolute; TOP: 48px"
runat="server"></asp:Label></form>
</body>
</HTML>
Now, the details on the codebehind
'''contents of up.aspx.cs
// contents of up.aspx.cs
namespace up
{
using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class upload : Page
{
protected Button cmdUpload;
protected DropDownList ddlPath;
protected HtmlInputFile File1;
protected Label Label1;
protected Label lblMessage;
protected Label lblPathMessage;
protected LinkButton LinkButton1;
private long lMaxFileSize = 10485760;
public static string sFileDir = "";
public DirectoryInfo[] subDirectories;
public void BindElement()
{
this.ddlPath.DataSource = this.subDirectories;
this.ddlPath.DataBind();
this.ddlPath.Items.Insert(0, "select from list");
}
private void cmdUpload_Click(object sender, EventArgs e)
{
if ((this.File1.PostedFile != null) && (this.File1.PostedFile.ContentLength > 0))
{
string fileName = Path.GetFileName(this.File1.PostedFile.FileName);
try
{
if (this.File1.PostedFile.ContentLength <= this.lMaxFileSize)
{
this.File1.PostedFile.SaveAs(sFileDir + fileName);
this.lblMessage.Visible = true;
this.lblMessage.Text = "File: " + sFileDir + fileName + " Uploaded Successfully";
}
else
{
this.lblMessage.Visible = true;
this.lblMessage.Text = "File Size if Over the Limit of " + this.lMaxFileSize;
}
}
catch (Exception)
{
this.lblMessage.Visible = true;
this.lblMessage.Text = "An Error Occured. Please Try Again!";
this.DeleteFile(sFileDir + fileName);
}
}
}
private void ddlPath_SelectedIndexChanged(object sender, EventArgs e)
{
sFileDir = sFileDir + this.ddlPath.SelectedValue.ToString() + @"\";
DirectoryInfo info = null;
info = new DirectoryInfo(sFileDir);
this.lblPathMessage.Text = "Directory is: " + sFileDir;
this.subDirectories = info.GetDirectories();
this.BindElement();
}
private void DeleteFile(string strFileName)
{
if (strFileName.Trim().Length > 0)
{
FileInfo info = new FileInfo(strFileName);
if (info.Exists)
{
info.Delete();
}
}
}
private void InitializeComponent()
{
this.cmdUpload.Click += new EventHandler(this.cmdUpload_Click);
this.ddlPath.SelectedIndexChanged += new EventHandler(this.ddlPath_SelectedIndexChanged);
this.LinkButton1.Click += new EventHandler(this.LinkButton1_Click);
base.Load += new EventHandler(this.Page_Load);
}
private void LinkButton1_Click(object sender, EventArgs e)
{
this.Reset_Root();
}
public void myPath()
{
sFileDir = base.Server.MapPath("../");
}
protected override void OnInit(EventArgs e)
{
this.InitializeComponent();
base.OnInit(e);
}
public void Page_Load(object sender, EventArgs e)
{
if (!base.IsPostBack)
{
this.Reset_Root();
}
}
public void Reset_Root()
{
sFileDir = "";
this.myPath();
this.subDirectories = new DirectoryInfo(sFileDir).GetDirectories();
this.BindElement();
}
}
}









A mate of mine, netguru -