Custom HTTP processing program display image (Asp.net 2.0)

Source: Internet
Author: User

Custom HTTP ProcessingProgramThat is, the HTTP handler must implement the interface system. Web. ihttphandler.
When implementing the ihttphandler interface, you must do two things: Setting the isreusable attribute and implementing the processrequest () method.
Create generic handler.

<% @ Webhandler Language = "C #" class = "handler" %>

Using system;
Using system. IO;
Using system. Web;

Public class handler: ihttphandler {

Public bool isreusable {
Get {
Return true;
}
}

Public void processrequest (httpcontext context ){
// Set up the response settings
Context. response. contenttype = "image/JPEG ";
Context. response. cache. setcacheability (httpcacheability. Public );
Context. response. bufferoutput = false;
// Setup the size parameter
Photosize size;
Switch (context. Request. querystring ["size"]) {
Case "S ":
Size = photosize. Small;
Break;
Case "M ":
Size = photosize. medium;
Break;
Case "L ":
Size = photosize. Large;
Break;
Default:
Size = photosize. Original;
Break;
}
// Setup the photoid Parameter
Int32 id =-1;
Stream stream = NULL;
If (context. Request. querystring ["photoid"]! = NULL & context. Request. querystring ["photoid"]! = ""){
Id = convert. toint32 (context. Request. querystring ["photoid"]);
Stream = photomanager. getphoto (ID, size );
} Else {
Id = convert. toint32 (context. Request. querystring ["albumid"]);
Stream = photomanager. getfirstphoto (ID, size );
}
// Get the photo from the database, if nothing is returned, get the default "Placeholder" photo
If (Stream = NULL) stream = photomanager. getphoto (size );
// Write image stream to the response stream
Const int buffersize = 1024*16;
Byte [] buffer = new byte [buffersize];
Int COUNT = stream. Read (buffer, 0, buffersize );
While (count> 0 ){
Context. response. outputstream. Write (buffer, 0, count );
Count = stream. Read (buffer, 0, buffersize );
}
}

}

Public static stream getphoto (INT photoid, photosize size ){
Using (sqlconnection connection = new sqlconnection (configurationmanager. connectionstrings ["personal"]. connectionstring )){
Using (sqlcommand command = new sqlcommand ("getphoto", connection )){
Command. commandtype = commandtype. storedprocedure;
Command. Parameters. Add (New sqlparameter ("@ photoid", photoid ));
Command. Parameters. Add (New sqlparameter ("@ size", (INT) size ));
Bool filter =! (Httpcontext. Current. User. isinrole ("friends") | httpcontext. Current. User. isinrole ("Administrators "));
Command. Parameters. Add (New sqlparameter ("@ ispublic", filter ));
Connection. open ();
Object result = command. executescalar ();
Try {
Return new memorystream (byte []) result );
} Catch {
Return NULL;
}
}
}
}

Public static stream getfirstphoto (INT albumid, photosize size ){
Using (sqlconnection connection = new sqlconnection (configurationmanager. connectionstrings ["personal"]. connectionstring )){
Using (sqlcommand command = new sqlcommand ("getfirstphoto", connection )){
Command. commandtype = commandtype. storedprocedure;
Command. Parameters. Add (New sqlparameter ("@ albumid", albumid ));
Command. Parameters. Add (New sqlparameter ("@ size", (INT) size ));
Bool filter =! (Httpcontext. Current. User. isinrole ("friends") | httpcontext. Current. User. isinrole ("Administrators "));
Command. Parameters. Add (New sqlparameter ("@ ispublic", filter ));
Connection. open ();
Object result = command. executescalar ();
Try {
Return new memorystream (byte []) result );
} Catch {
Return NULL;
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.