DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   ASP.NET (http://www.daniweb.com/forums/forum18.html)
-   -   Custom HttpHandler. (http://www.daniweb.com/forums/thread67389.html)

khusani Jan 15th, 2007 10:27 am
Custom HttpHandler.
 
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.

plazmo Jan 23rd, 2007 1:41 pm
Re: Custom HttpHandler.
 
Quote:

Originally Posted by khusani (Post 301726)
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.

sanjaycedti Oct 27th, 2009 5:40 am
you are overriding default handler for .aspx. use different extension rather than using .aspx


All times are GMT -4. The time now is 3:58 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC