[Help] Why is it wrong to use ashx to read images from northwind?

Source: Internet
Author: User

Code reference self http://blog.joycode.com/liuhuimiao/archive/2005/06/03/52554.aspx? Pending = true

1 <% @ webhandler Language = "C #" class = "handler" %>
2
3 using system;
4 using system. IO;
5 using system. Web;
6
7 public class handler: ihttphandler {
8
9 Public bool isreusable {
10 get {
11 return true;
12}
13}
14
15 public void processrequest (httpcontext context ){
16 // set up the response settings
17 context. response. contenttype = "image/JPEG ";
18 context. response. cache. setcacheability (httpcacheability. Public );
19 context. response. bufferoutput = false;
20
21
22 // setup the photoid Parameter
23 int32 id =-1;
24 stream = NULL;
25 if (context. Request. querystring ["photoid"]! = NULL & context. Request. querystring ["photoid"]! = ""){
26 id = convert. toint32 (context. Request. querystring ["photoid"]);
27 stream = (getphoto (ID ));
28} else {
29 return;
30}
31 // write image stream to the response stream
32 const int buffersize = 1024*16;
33 byte [] buffer = new byte [buffersize];
34 int COUNT = stream. Read (buffer, 0, buffersize );
35 while (count> 0 ){
36 context. response. outputstream. Write (buffer, 0, count );
37 COUNT = stream. Read (buffer, 0, buffersize );
38}
39}
40 public stream getphoto (INT photoid)
41 {
42 sqlconnection myconnection = new sqlconnection (configurationmanager. connectionstrings ["personal"]. connectionstring );
43 sqlcommand mycommand = new sqlcommand ("select [Photo] from [employees] Where [employeeid] = @ employeeid", myconnection );
44 mycommand. commandtype = commandtype. text;
45 mycommand. Parameters. Add (New sqlparameter ("@ employeeid", photoid ));
46 myconnection. open ();
47 object result = mycommand. executescalar ();
48
49 try
50 {
51 return New memorystream (byte []) result );
52}
53 catch (argumentnullexception E)
54 {
55 return NULL;
56}
57 finally
58 {
59 myconnection. Close ();
60}
61}
62
63}

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.