Introduction
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.
Background
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//";
if (context.Request.Files.Count <= 0)
{
context.Response.Write("No file uploaded");
}
else
{
for (int i = 0; i < context.Request.Files.Count; ++i)
{
HttpPostedFile file = context.Request.Files[i];
file.SaveAs(context.Server.MapPath(filePath+file.FileName));
context.Response.Write("File uploaded");
}
}
}
Using the code
- Your web site/application will need a user interface such as an ASPX page, a generic HttpHandler, and a web.config configuration file.
- On the ASPX page, add a file upload control and a button control, and set the
PostBackUrl
to point to the handler's type.
<asp:FileUpload ID="FileUpload" runat="server" />
<asp:Button ID="But_Upload" runat="server"
Text="Upload" PostBackUrl=".upl" />
- You will then need to configure the handler in the web.config.
<httphandlers>
<add verb="*" path=".upl"
type="UploadHttphandler.UploadHandler,UploadHttphandler">
</add>
</httphandlers>
- 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 http://jumploader.com/download.html. You will need the following code in the ASPX page to render the applet:
<applet width="600" height="400"
archive="jumploader_z.jar"
code="jmaster.jumploader.app.JumpLoaderApplet.class"
name="jumpLoaderApplet">
<param value="/.upl" name="uc_uploadUrl" />
</applet>
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" />