Tags


File Upload Control:

Use to allow user to select and upload a single file to the server.

Inheritance: WebConctrol àFile Upload

User can select a file or type a file path.

Properties:

  • FileBytes: The file is exposed as a byte array
  • FileContent: The file is exposed as a stream
  • FileName:
  • HasFile: True, if the file path has a file.
  • PostedFile: The file is exposed as an object of type HttpPostedFile.
    • HttpPostedFile has two properties:
      • ContentType
      • ContentLength

Methods:

  • AddAttributesToRender:
  • New:
  • OnPreRender:
  • Render:
  • SaveAS: To save the file to server.  Can be applied to the FileUpload control of HttpPostedFile object.

Location to save:

By default, the requiredRootedSaveAsPath attribute of the httpRuntime config element in the Web.config is set true. So, we need to provide an absolute path to save the file.

We can get absolute path using MapPath method of the HttpServerUtility class and passing to the method the tilde (~) operator, which means application root folder. In my code below I added a folder to store uploaded file. By Clicking VS solution and “Add new” then “AddFolder”.

Max file size to Upload:

Max file size depend on the value of MaxRequestLength attribute of the httpRuntime in Web.config file.

ASPX CODE:

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”FileUpload.aspx.cs” Inherits=”MctsWeb.FileUpload” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml&#8221; >

<head runat=”server”>

<title></title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<asp:FileUpload ID=”fuUpload” runat=”server” Font-Names=”Calibri”

Font-Size=”Small” ForeColor=”#3399FF” BorderStyle=”Groove” />

</div>

<asp:Button ID=”btnUpload” runat=”server” Font-Bold=”True” Font-Names=”Calibri”

Font-Size=”Small” ForeColor=”#3399FF” onclick=”btnUpload_Click”

Text=”Upload File” Width=”76px” />

<p>

<asp:Label ID=”lblFileInfo” runat=”server” Font-Bold=”True”

Font-Names=”Calibri” Font-Size=”Small”></asp:Label>

</p>

</form>

</body>

</html>

Code Behind:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace MctsWeb

{

public partial class FileUpload : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btnUpload_Click(object sender, EventArgs e)

{

if (fuUpload.HasFile)

{

lblFileInfo.Text =””;

lblFileInfo.Text += “File Length:” +

fuUpload.FileBytes.Length + “<br/>” +

“File Name:” +

fuUpload.FileName + “<br/>” +

“File Content:” +

fuUpload.FileContent + “<br/>” +

“Content Length:” +

fuUpload.PostedFile.ContentLength + “<br/>” +

“Content Type” +

fuUpload.PostedFile.ContentType + “<br/>” +

“Posted File Name:” +

fuUpload.PostedFile.FileName + “<br/>”;

fuUpload.SaveAs(MapPath(“~/Upload/” + fuUpload.FileName));

}

else

{

lblFileInfo.Text = “There is no valid file to upload”;

}

}

}

}

Output:

FileUpload

Advertisements