天天看點

ashx檔案的使用[轉]

ashx檔案,我們可用用來作圖檔加載(在之前我們一般使用aspx或者webservice去做),一般做法如下:

handler.ashx:

ashx檔案的使用[轉]

 1

ashx檔案的使用[轉]

<%@ webhandler language="c#" class="handler" %>

 2

ashx檔案的使用[轉]

using system;

 3

ashx檔案的使用[轉]

using system.io;

 4

ashx檔案的使用[轉]

using system.web;

 5

ashx檔案的使用[轉]

public class handler : ihttphandler {

 6

ashx檔案的使用[轉]

 7

ashx檔案的使用[轉]

public bool isreusable {

 8

ashx檔案的使用[轉]

  get {

 9

ashx檔案的使用[轉]

   return true;

10

ashx檔案的使用[轉]

  }

11

ashx檔案的使用[轉]

}

12

ashx檔案的使用[轉]

public void processrequest (httpcontext context) {

13

ashx檔案的使用[轉]

  context.response.contenttype = "image/jpeg";

14

ashx檔案的使用[轉]

  context.response.cache.setcacheability(httpcacheability.public);

15

ashx檔案的使用[轉]

  context.response.bufferoutput = false;

16

ashx檔案的使用[轉]

  photosize size;

17

ashx檔案的使用[轉]

  switch (context.request.querystring["size"]) {

18

ashx檔案的使用[轉]

   case "s":

19

ashx檔案的使用[轉]

    size = photosize.small;

20

ashx檔案的使用[轉]

    break;

21

ashx檔案的使用[轉]

   case "m":

22

ashx檔案的使用[轉]

    size = photosize.medium;

23

ashx檔案的使用[轉]

24

ashx檔案的使用[轉]

   case "l":

25

ashx檔案的使用[轉]

    size = photosize.large;

26

ashx檔案的使用[轉]

27

ashx檔案的使用[轉]

   default:

28

ashx檔案的使用[轉]

    size = photosize.original;

29

ashx檔案的使用[轉]

30

ashx檔案的使用[轉]

  } 

31

ashx檔案的使用[轉]

  int32 id = -1;

32

ashx檔案的使用[轉]

  stream stream = null;

33

ashx檔案的使用[轉]

  if (context.request.querystring["photoid"] != null && context.request.querystring["photoid"] != "") {

34

ashx檔案的使用[轉]

   id = convert.toint32(context.request.querystring["photoid"]);

35

ashx檔案的使用[轉]

   stream = photomanager.getphoto(id, size);

36

ashx檔案的使用[轉]

  } else {

37

ashx檔案的使用[轉]

   id = convert.toint32(context.request.querystring["albumid"]);

38

ashx檔案的使用[轉]

   stream = photomanager.getfirstphoto(id, size);

39

ashx檔案的使用[轉]

40

ashx檔案的使用[轉]

  if (stream == null) stream = photomanager.getphoto(size);

41

ashx檔案的使用[轉]

  const int buffersize = 1024 * 16;

42

ashx檔案的使用[轉]

  byte[] buffer = new byte[buffersize];

43

ashx檔案的使用[轉]

  int count = stream.read(buffer, 0, buffersize);

44

ashx檔案的使用[轉]

  while (count > 0) {

45

ashx檔案的使用[轉]

   context.response.outputstream.write(buffer, 0, count);

46

ashx檔案的使用[轉]

   count = stream.read(buffer, 0, buffersize);

47

ashx檔案的使用[轉]

48

ashx檔案的使用[轉]

49

ashx檔案的使用[轉]

50

ashx檔案的使用[轉]

51

ashx檔案的使用[轉]
ashx檔案的使用[轉]

*.aspx:

<img src="myhttphander.ashx?id=123" width="20" height="20" />

我們變通以下,發現其實除了可以輸出圖檔以外,還可以輸出文字:

ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]

    public void processrequest (httpcontext context) {

ashx檔案的使用[轉]

        context.response.contenttype = "text/plain";

ashx檔案的使用[轉]

        context.response.write("alert('hi')");

ashx檔案的使用[轉]

    }

ashx檔案的使用[轉]
ashx檔案的使用[轉]

    public bool isreusable {

ashx檔案的使用[轉]

        get {

ashx檔案的使用[轉]

            return false;

ashx檔案的使用[轉]

        }

ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]

彈出alert

<script src="handler.ashx"></script>

也可以把.ashx當成css檔案

<link href="css/handler.ashx" rel="stylesheet" type="text/css">

xml檔案

orderdoc.load("handler.ashx");

還可以嵌入文字:

ashx檔案的使用[轉]
ashx檔案的使用[轉]

<%@ webhandler language="c#" class="testhandler" %>

ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]

public class testhandler : ihttphandler {

ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]

        context.response.write("document.write(\"hello world\");");

ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]
ashx檔案的使用[轉]

<script type="text/javascript" src="testhandler.ashx" />

歡迎加群互相學習,共同進步。qq群:ios: 58099570 | android: 330987132 | go:217696290 | python:336880185 | 做人要厚道,轉載請注明出處!http://www.cnblogs.com/sunshine-anycall/archive/2009/07/08/1519022.html