ASP.NET 進階編程基礎第十篇—HttpHandler處理

來源:互聯網
上載者:User

前言:好幾天沒有寫部落格了,今天繼續寫我們的asp.net進階編程基礎系列的第十篇,Httphandler處理,通過這篇部落格我們可以瞭解到Httphandler是個幹什麼的了,當然本來想寫寫這個底層的,但是想想我們可以自己查一下,所以就沒有寫了,希望我們能夠共同進步。

  1. HttpHandler處理

(1) HttpHandler是對請求的響應,可以輸出普通的Html內容,也可以輸出圖片,也可以輸出一個檔案。

(2) 案例1:圖片中顯示訪問者的資訊

建立一個ashx項目,起名為訪問者資訊.ashx項目,在裡面寫入如下代碼:

 1      context.Response.ContentType = "Image/JPEG"; 2  3      using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(300, 300)) 4  5      { 6  7           using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) 8  9           {10 11               g.DrawString("IP地址是:" + context.Request.UserHostAddress, new System.Drawing.Font("宋體", 20), System.Drawing.Brushes.Red, 0, 0);12 13               g.DrawString("作業系統:" + context.Request.Browser.Platform, new System.Drawing.Font("宋體", 20), System.Drawing.Brushes.Red, 0, 50);14 15               g.DrawString("瀏覽器資訊:" + context.Request.Browser.Type, new System.Drawing.Font("宋體", 20), System.Drawing.Brushes.Red, 0, 100);16 17            }18 19     bitmap.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);20 21        }

 

(3) 案例2:填入朋友的名字,就能夠產生要惡搞的圖片串連

1)建立一個一般處理常式.ashx,在其中寫入如下代碼,驗證的話在瀏覽器輸入參數。

   

 1      context.Response.ContentType = "Image/JPEG"; 2  3         string name = context.Request["Name"]; 4  5         string fullPath = HttpContext.Current.Server.MapPath("劉亦菲.jpg"); 6  7         using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fullPath)) 8  9         {10 11             using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))12 13             {14 15                 g.DrawString(name, new System.Drawing.Font("宋體", 20), System.Drawing.Brushes.Red, 113, 35);16 17             }18 19             bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);20 21         }

 

  1. HttpHandler實現檔案下載

(1) 如果HttpHandler輸出的是HTML,txt,jpeg等類型的資訊,那麼瀏覽器會直接顯示,如果希望彈出儲存對話方塊,則需要添加

Header:string encodeFileName=HttpUtility.UrlEncode(“過濾詞.txt”);

Response.AddHeader(”content-Disposition”,string.Format(”attachment;filename=\”{0}\””,encodeFileName));其中filename後面為編碼後的檔案名稱,filename段為建議的儲存檔案名稱。

(2) 1) 建立一個下載.html頁面,寫入如下代碼:

   <a href="劉亦菲.jpg">圖片1</a>

 2) 建立一個1.txt檔案,亂寫一些東西,在下載頁面中寫入如下代碼:

   <a href="1.txt">txt下載</a>

 3) 建立一個一般處理常式下載.ashx

        context.Response.ContentType = "Image/JPEG";

        context.Response.AddHeader("content-Disposition", "attachment:filename=劉亦菲.jpg");

        context.Response.WriteFile("劉亦菲.jpg");

 4) 在下載.htm中寫入如下代碼

<a href="下載.ashx">下載</a>

(3) 動態輸出用處,不要再把資源儲存到磁碟上面在輸出(不會有檔案重名的問題,檔案不產生在伺服器端)。案例:點選連結彈出圖片下載對話方塊,web原則,能直接將產生的內容以流的形式輸出給瀏覽器,就不要產生臨時檔案夾,

(4) 用NPOI動態產生一個Excel表然後彈出對話方塊讓使用者下載,檔案名稱是:”使用者列表.xls”。

 1) 建立一個asp.net web應用程式起名為匯出excel.aspx,建立一個BLL檔案夾,下載NPOI的所有dll檔案加到檔案夾下面,添加dll引用。

 <a href="DownLoadExcel.ashx">下載Excel</a>

2) 建立一個一般處理常式DownLoadExcel.ashx,在裡面寫入如下代碼:

  

 1  context.Response.ContentType = "application/x-excel"; 2  3    string filename = HttpUtility.UrlEncode("動態資料.xls"); 4  5    context.Response.AddHeader("content-Disposition", "attachment:filename=" + filename); 6  7    HSSFWorkbook workbook = new HSSFWorkbook(); 8  9    HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();10 11    HSSFRow row = (HSSFRow)sheet.CreateRow(0);12 13    HSSFCell cell = row.CreateCell(0, HSSFCell.CELL.TYPE.STRING);14 15    row.setCellValue("Hello");16 17    row.CreateCell(0, HSSFCell.CELL.TYPE.STRING).SetCellValue(3.14);18 19    workbook.Write(context.Response.OutputStream);

 

  1. ASP.NET補充問題

(1) WebApplication每次修改以後點擊[產生解決方案],也能立即看到修改結果,不需要重新啟動瀏覽器。原理:產生以後才將變化的部分產生dll,而webSite則是每次訪問頁面的時候會檢查cs檔案是否變化了,變化了則重新自動編譯,所以每次修改以後都會立即有效果的。

(2) 方便開發不用每次調試都設定起始頁,在項目中的選項中設定[web]—>啟動操作—>當前頁面—>這樣當前啟用的頁面就是起始頁。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.