今天逛博客園,無意發現一篇好文章,關於ashx文件的使用。
本文轉載:http://www.cnblogs.com/yzenet/p/3470388.html
<script type="text/javascript"> $(function () { $("#btnsave").click(function () { var json = { "age": 12, "address": "hk", "ship": [ { "custID": "sz", "cpname": "bookstrf" },{ "custID": "fkff", "cpname": "kfg"}] }; $.ajax({ type: "post", url: "Handler/test.ashx", datatype: "json", data: { name: JSON.stringify(json) }, async: true, success: function (data) { alert(data); } }); }); }); </script>
public class test : IHttpHandler { public void ProcessRequest(HttpContext context) { string ss = context.Request["name"]; var serialize = new JavaScriptSerializer(); var t = serialize.Deserialize<Model>(ss); } public bool IsReusable { get { return false; } } class Model { public int age {get; set;} public string address{get; set;} public List<Ship> ship{get; set;} } class Ship { public string custID{get; set;} public string cpname { get; set; } } }
//得到數據對象:
請求URL:http://localhost:3043/Handler1.ashx?name=HelloWorld!
本文博客轉載:http://www.cnblogs.com/travelcai/archive/2007/09/25/904767.html
總結:看了上面文章一和文章二;可以將兩者結合起來。
如果是顯示多個不同圖片時候,是否可以將
<asp:Image ID="Image1" runat="server" ImageUrl="~/ImageHandler.ashx"/></div>
進行改造:
<asp:Image ID="Image1" runat="server" ImageUrl="~/ImageHandler.ashx/?pic=U1513.jpg"/></div>
ashx一般程序代碼:
public void ProcessRequest (HttpContext context) { //獲取虛擬目錄的物理路徑。 string path = context.Server.MapPath(""); //獲取圖片文件名 string picFileName = context.Request["pic"]; //獲取圖片文件的二進制數據。 byte[] datas = System.IO.File.ReadAllBytes(path + "\\" + picFileName); //將二進制數據寫入到輸出流中。 context.Response.OutputStream.Write(datas, 0, datas.Length); }
繼續擴展一下思路:如果是讀取員工信息表,顯示員工頭像呢?是否只需要傳遞EmpID就行呢? 。。。 。。。 。答案是可以的!!!