Search code examples
asp.nethttphandler

HttpHandler doesn't render image on server


I have a FileHandler.ashx which implements IHttpHandler. The only thing this handler does is get the file and return it thru context.Response.

It works fine in my local unit even after publishing it (in my local machine). While on the server machine it only works if I run the website thru the Visual Studio, it doesn't work if I access the published website (i.e. http://url.to.site/).

This is what it looks like when running on my local machine (thru Visual Studio or published site), and thru Visual Studio on the server machine: enter image description here

And this is what it looks like when accessing the published website on the server machine:enter image description here

This is a snippet from the ProcessRequest method of the FileHandler:

var strExtenstion = Path.GetExtension(file);
var fileInfo = new FileInfo(file);

context.Response.Clear();
context.Response.ClearContent();
context.Response.AppendHeader("content-length", file.Length.ToString());

if (strExtenstion == ".pdf") { context.Response.ContentType = "application/pdf"; }
else if (strExtenstion == ".png") { context.Response.ContentType = "image/png"; }
else if (strExtenstion == ".jpg") { context.Response.ContentType = "image/jpeg"; }
else if (strExtenstion == ".gif") { context.Response.ContentType = "image/gif"; }
else if (strExtenstion == ".bmp") { context.Response.ContentType = "image/bmp"; }
else if (strExtenstion == ".txt") { context.Response.ContentType = "text/plain"; }
else {
     context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
     context.Response.ContentType = "application/octet-stream";
}

context.Response.WriteFile(fileInfo.FullName);

file is the path of the file.

** UPDATE **

If this would help: the server I used is a GoDaddy server which is maintained using Plesk.


Solution

  • I found out the answer: I used the wrong variable in this line:

    context.Response.AppendHeader("content-length", file.Length.ToString());
    

    file.Length.ToString() should've been fileInfo.Length.ToString(), but this code shouldn't have worked when run thru VS, strange.