Click here to Skip to main content
15,358,266 members
Articles / Web Development / HTML
Posted 8 Dec 2009


37 bookmarked

Upload files using an HttpHandler

Rate me:
Please Sign up or sign in to vote.
4.32/5 (19 votes)
9 Dec 2009CPOL2 min read
Using an HttpHandler to upload a file.


Using an HttpHandler to upload files can be quite handy while designing multiple file uploads, large file uploads, resumable file uploads, and reporting on the progress of an upload. I’m sure there are many more uses for it. The article does not intend on explaining the implementations of all uses of the HttpHandler in uploading files, but rather explains the code required by the HttpHandler to upload files. That being said, I have still included an example on how to upload multiple files using the HttpHandler with the help of the Jumploader Java applet in the Points of interest section.


After searching around and being unable to find a short, clear, and concise code snippet that could do this, I wrote my own, and would like to share it with anyone who needs it.

Code anatomy

I am assuming you are familiar with the use of an HttpHandler. Once the file is selected with the file upload control, the button does a post to the HttpHandler. The handler processes the request from the button and uses the HttpPostedFile class to receive the uploaded file class and save it on the server. The HttpPostedFile class provides the properties and methods to get information about an individual file and to read and save the file, and belongs to the System.Web namespace of the .NET framework. Most of the code is contained within the ProcessRequest method of the handler. In the ProcessRequest method, we iterate through the Request.Files array and save them to the local folder using the FileSaveAs method of the HttpPostedFile class, it’s that simple. A Response is populated with the status of the upload and sent back to the client.

public void ProcessRequest(HttpContext context)
    string filePath = "FileSave//";

    //write your handler implementation here.
    if (context.Request.Files.Count <= 0)
        context.Response.Write("No file uploaded");
        for (int i = 0; i < context.Request.Files.Count; ++i)
            HttpPostedFile file = context.Request.Files[i];
            context.Response.Write("File uploaded");

Using the code

  1. Your web site/application will need a user interface such as an ASPX page, a generic HttpHandler, and a web.config configuration file.
  2. On the ASPX page, add a file upload control and a button control, and set the PostBackUrl to point to the handler's type.
  3. HTML
    <asp:FileUpload ID="FileUpload"  runat="server" />
        <asp:Button ID="But_Upload" runat="server"
                Text="Upload" PostBackUrl=".upl" />
  4. You will then need to configure the handler in the web.config.
  5. XML
      <add verb="*" path=".upl"
  6. Lastly, modify the method ProcessRequest in your HttpHandler class to include the code snippet under the code anatomy section.

The download includes a simple file upload control that posts back to the upload handler. You can do the same with whatever interface that you wish to use to upload files. You could also modify the file on receiving it, or generate thumbnails etc., using the file.

Points of interest

Multiple file uploader – You can use Jumploader, a very cool multiple file upload Java applet, with the above code. You will need to include the applet jar file in your project, which can be downloaded from You will need the following code in the ASPX page to render the applet:

<applet width="600" height="400"

    <param value="/.upl" name="uc_uploadUrl" />

You can increase the file size being uploaded by adding the following to your web.config. The executionTimeout is in seconds, and maxRequestLength is in bytes.

<httpRuntime executionTimeout="1800" maxRequestLength="100000" />


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ritesh Ramesh
Architect Infosolvex Solutions Inc
Australia Australia
Ritesh is an IT consultant with over ten years of experience in the IT industry varying from consultation, architecture, design, development to technical management. He has a strong background in solutions and applications architecture with a focus on Microsoft’s .Net platform. His area of expertise spans design and implementation of client/server, database and web-based systems. He has worked with C#, ASP.NET 1.1 and 2.0, ADO.NET, Web Services and SQL technology on several enterprise class projects.

Freedom is not worth having if it does not include the freedom to make mistakes.
Mahatma Gandhi

Comments and Discussions

GeneralMy vote of 1 Pin
Dave Johnston13-Jun-14 0:44
MemberDave Johnston13-Jun-14 0:44 
QuestionNot Working :( Pin
sacp.net16-May-12 10:10
Membersacp.net16-May-12 10:10 
QuestionGood Pin
Member 773936625-Jan-12 4:20
MemberMember 773936625-Jan-12 4:20 
QuestionCan you do this without an aspx file? Pin
ProProgrammer7777-Oct-11 15:49
MemberProProgrammer7777-Oct-11 15:49 
GeneralMy vote is 5 Pin
Ehsan Baghaki24-Dec-10 8:27
MemberEhsan Baghaki24-Dec-10 8:27 
General[My vote of 1] yes, totally crap!! Pin
M8R-3d3rjt125-Dec-09 6:37
MemberM8R-3d3rjt125-Dec-09 6:37 
GeneralRe: [My vote of 1] yes, totally crap!! Pin
Ritesh Ramesh25-Dec-09 23:48
MemberRitesh Ramesh25-Dec-09 23:48 
Jokeok, but newbs need more explaination... Pin
William Forney15-Dec-09 3:27
MemberWilliam Forney15-Dec-09 3:27 
GeneralRe: ok, but newbs need more explaination... Pin
Ritesh Ramesh16-Dec-09 10:23
MemberRitesh Ramesh16-Dec-09 10:23 
QuestionWhat is the maximum size u can load? Pin
G.Srinivasan14-Dec-09 19:23
MemberG.Srinivasan14-Dec-09 19:23 
AnswerRe: What is the maximum size u can load? Pin
Ritesh Ramesh14-Dec-09 22:47
MemberRitesh Ramesh14-Dec-09 22:47 
Questioncan use multi thread to upload? Pin
reborn_zhang9-Dec-09 7:44
Memberreborn_zhang9-Dec-09 7:44 
Generalerror Pin
Nitin S9-Dec-09 0:45
professionalNitin S9-Dec-09 0:45 
GeneralRe: error Pin
Ritesh Ramesh9-Dec-09 0:52
MemberRitesh Ramesh9-Dec-09 0:52 
GeneralRe: error Pin
Nitin S9-Dec-09 0:53
professionalNitin S9-Dec-09 0:53 
GeneralRe: error Pin
Ritesh Ramesh9-Dec-09 0:59
MemberRitesh Ramesh9-Dec-09 0:59 
GeneralMy vote of 1 Pin
Michael E. Jones8-Dec-09 23:45
MemberMichael E. Jones8-Dec-09 23:45 
GeneralRe: My vote of 1 Pin
Ritesh Ramesh9-Dec-09 1:00
MemberRitesh Ramesh9-Dec-09 1:00 
GeneralMy vote of 1 Pin
Rogic8-Dec-09 15:43
MemberRogic8-Dec-09 15:43 
GeneralRe: My vote of 1 Pin
gstolarov11-Dec-09 4:06
Membergstolarov11-Dec-09 4:06 
GeneralMy vote of 2 Pin
Webnoi8-Dec-09 14:58
MemberWebnoi8-Dec-09 14:58 
GeneralRe: My vote of 2 Pin
Ritesh Ramesh8-Dec-09 23:03
MemberRitesh Ramesh8-Dec-09 23:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.