get data from a blob attributes

1 view
Skip to first unread message

Frank Lodeiro

unread,
Aug 4, 2008, 10:12:12 PM8/4/08
to
Hi I am doing an IHttpHandler to donwload a file, from client I pass the
externalId, the doubts I am missing something, because it code don't work?

regards
Frank

public void ProcessRequest(HttpContext context)
{
try {
string result = string.Empty;

string fileExternalId = context.Request["fileId"];
if (context.Session["UserLoginId"] != null){
if (fileExternalId != null & fileExternalId != ""){
//getting object
IObjectInstance aIFile = ObjectForId(fileExternalId);
MailAttachment aMailAttachment = aIFile.AsObject as MailAttachment;
context.Response.ContentType = aMailAttachment.ContentType;
//getting blob
byte[] blobData = (byte[])aMailAttachment.File;
context.Response.BinaryWrite(blobData);
}
}
else
{
context.Response.StatusCode = 402;
context.Response.StatusDescription = "Session expired";
}
}
catch (Exception e)
{
context.Response.StatusCode = 403;
string errorInfo =
"<span style='font-weight: bold; color: rgb(255, 0, 0);
background-color: rgb(255, 255, 153);'>Offending URL: </span>" +
"<span style='background-color: rgb(255, 255, 153);'>" +
context.Request.Url.ToString () + "</span>" + "<br><br><br>" +
"<span style='font-weight: bold; color: rgb(255, 0, 0);
background-color: rgb(255, 255, 153);'>Source: </span>" +
"<span style='background-color: rgb(255, 255, 153);'>" + e.Source +
"</span>" + "<br><br><br>" +
"<span style='font-weight: bold; color: rgb(255, 0, 0);
background-color: rgb(255, 255, 153);'>Message: </span>" +
"<span style='background-color: rgb(255, 255, 153);'>" + e.Message +
"</span>" + "<br><br><br>" +
"<span style='font-weight: bold; color: rgb(255, 0, 0);
background-color: rgb(255, 255, 153);'>Stack trace: </span>" +
"<span style='background-color: rgb(255, 255, 153);'>" + e.StackTrace +
"</span>" + "<br><br><br>";
context.Response.Write(errorInfo);

}
}


Dmitriy Nagirnyak

unread,
Aug 4, 2008, 7:47:40 PM8/4/08
to
Hello Frank,

> because it code don't work?
>

First of all it would be useful to tell what exactly doesn't work to make
our life easier and save time.

OK.
Lokking at the code I can see following issues:
1. Session is not available in HttpHandler. See how to enable it: http://www.hanselman.com/blog/GettingSessionStateInHttpHandlersASHXFiles.aspx

2. I don't know why you are casting here:


byte[] blobData = (byte[])aMailAttachment.File;

If MailAttachment.File is byte[] then you don't need it, if it's a string
you have to use Encoding.GetBytes to convert string to byte array.


3. HttpCode 402 is described as "Payment Required - This code is reserved
for future use.". So I'm not sure this is the correct response code if the
user is not logged in (401 Unauthorized might probably be better).

4. Catching exception you set error code to 403 (Forbidden). An error happened
on the server so 500 Internal Server Error seems to be a bit more correct.

5. Not an issue but suggestion. If you format code in html (e.StackTrace)
you should use "code" and/or "pre" tags instead os formatting it with span.


Anyaway I don't see big issue except session. But you'd better provide us
more details.

Cheers,
Dmitriy.


Reply all
Reply to author
Forward
0 new messages