944,033 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 9548
  • ASP.NET RSS
Jan 15th, 2007
0

Custom HttpHandler.

Expand Post »
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>
<addverb="*"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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khusani is offline Offline
13 posts
since Oct 2006
Jan 23rd, 2007
0

Re: Custom HttpHandler.

Click to Expand / Collapse  Quote originally posted by khusani ...
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>
<addverb="*"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.
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Oct 27th, 2009
0
Re: Custom HttpHandler.
you are overriding default handler for .aspx. use different extension rather than using .aspx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sanjaycedti is offline Offline
3 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: check date
Next Thread in ASP.NET Forum Timeline: does C# Window Service supports Browser_DocumentCompleted event of Web browser?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC