I am developing a multi file uploader user control.
after researching for it. the trick is to build a custom HttpHandler to perform the upload process from the context.request.files.

here is the code for my handler:

namespace PSUpload
{
/// <summary>
/// Custom HttpHandler to perform uploading files from client
/// to server.
/// </summary>
public class PSUploader : IHttpHandler
{
#region Member Variables
private string dirPath = string.Empty;
private DirectoryInfo dir = null;
private string baseLocation = string.Empty;
#endregion
#region Constructor
/// <summary>
/// constructor, assing the dirPath string to the AttachPath
/// which is set in the web.config.
/// instantiate the dir DirectoryInfo to check if the directory exist
/// or not.
/// assing the baselocation string to the DirectoryInfo dir.
/// </summary>
public PSUploader()
{
dirPath = ConfigurationSettings.AppSettings"AttachPath"].ToString();
dir = new DirectoryInfo(dirPath);
//create the directory if it does not exist.
if (!dir.Exists)
{
dir.Create();
}

baseLocation = dir.ToString();
}
#endregion

#region ProcessRequest
/// <summary>
/// called when a page is requested.
/// foreach posted file in the context. save the file to server(upload it).
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
for (int indx = 0 ; indx < context.Request.Files.Count ; indx++)
{
HttpPostedFile file = context.Request.Files[indx];
if (file.ContentLength > 0)
{
file.SaveAs(baseLocation + file.FileName);
}
}
}
}
#endregion

#region IsReusable
public bool IsReusable
{
get
{
return true;
}
}
#endregion
}
}

after that i have registered my handler in the web.config file using the following line:

<httpHandlers>
<add verb="*" path="*.aspx" type="PSUpload.PSUploader,PSUpload"/>
</httpHandlers>

the problem is when i run the application, i just get an empty page without the user control or anything. when i view source code from the browser. i just get the following:

<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1256"></HEAD>
<BODY></BODY></HTML>

i dont know what the problem is.

does anyone has a clue of what the problem seems to be? is there anything missing in my code?

please help
thanx.

Recommended Answers

All 2 Replies

I am developing a multi file uploader user control.
after researching for it. the trick is to build a custom HttpHandler to perform the upload process from the context.request.files.

here is the code for my handler:

namespace PSUpload
{
/// <summary>
/// Custom HttpHandler to perform uploading files from client
/// to server.
/// </summary>
public class PSUploader : IHttpHandler
{
#region Member Variables
private string dirPath = string.Empty;
private DirectoryInfo dir = null;
private string baseLocation = string.Empty;
#endregion
#region Constructor
/// <summary>
/// constructor, assing the dirPath string to the AttachPath
/// which is set in the web.config.
/// instantiate the dir DirectoryInfo to check if the directory exist
/// or not.
/// assing the baselocation string to the DirectoryInfo dir.
/// </summary>
public PSUploader()
{
dirPath = ConfigurationSettings.AppSettings"AttachPath"].ToString();
dir = new DirectoryInfo(dirPath);
//create the directory if it does not exist.
if (!dir.Exists)
{
dir.Create();
}

baseLocation = dir.ToString();
}
#endregion

#region ProcessRequest
/// <summary>
/// called when a page is requested.
/// foreach posted file in the context. save the file to server(upload it).
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
for (int indx = 0 ; indx < context.Request.Files.Count ; indx++)
{
HttpPostedFile file = context.Request.Files[indx];
if (file.ContentLength > 0)
{
file.SaveAs(baseLocation + file.FileName);
}
}
}
}
#endregion

#region IsReusable
public bool IsReusable
{
get
{
return true;
}
}
#endregion
}
}

after that i have registered my handler in the web.config file using the following line:

<httpHandlers>
<add verb="*" path="*.aspx" type="PSUpload.PSUploader,PSUpload"/>
</httpHandlers>

the problem is when i run the application, i just get an empty page without the user control or anything. when i view source code from the browser. i just get the following:

<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1256"></HEAD>
<BODY></BODY></HTML>

i dont know what the problem is.

does anyone has a clue of what the problem seems to be? is there anything missing in my code?

please help
thanx.

This is what asp.net pages display when there is no output. And for this hander you are not displaying anything on the page.
If you want to put something on the page either do a Response.Write("") or use a regular aspx page with controls.

you are overriding default handler for .aspx. use different extension rather than using .aspx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.