前言:
在ASP.NET WebForm時要顯示二進位圖片或是加工後的圖片,通常會將Image的路徑指定為ashx(泛型處理常式),雖然也可以在MVC網站中加入ashx,但還是覺的怪怪的,不太符合MVC的精神,因為回應資訊通常是透過action來傳回ActionResult物件給View.
仔細研究ActionResult後你會發現MVC Framework並沒有提供用來回應圖片資訊的ActionResult類別,所以我們就自已動手做吧!!
實作:
按照慣例一樣使用北風資料庫來讀取員工的二進位圖片XD
1.在網站根目錄下建立一個資夾料名為Helper,在其中加入一個類別檔名為MyActionResult.
![](http://Files.Dotblogs.com.tw/pbnttttt/0911/2009111419822895.png)
2.在類別檔中建立一個繼承自ActionResult(抽象類別)的類別名為ImageResult,接著覆寫ExecuteResult方法
02 | using System.Collections.Generic; |
08 | namespace ShowImageSample.Helper |
10 | public class ImageResult : ActionResult |
12 | private MemoryStream _imgStream; |
13 | private string _contentType; |
15 | public ImageResult(MemoryStream imgStream, string contentType) |
17 | _imgStream = imgStream; |
18 | _contentType = contentType; |
21 | public override void ExecuteResult(ControllerContext context) |
24 | throw new ArgumentException( "context" ); |
25 | if (_imgStream == null ) |
26 | throw new ArgumentException( "imgStream is null" ); |
27 | if (_contentType == null ) |
28 | throw new ArgumentException( "contentType is null" ); |
30 | HttpResponseBase response = context.HttpContext.Response; |
33 | response.Cache.SetCacheability(HttpCacheability.NoCache); |
34 | response.ContentType = _contentType; |
36 | _imgStream.WriteTo(response.OutputStream); |
3.在Model資料夾下建立一個Linq To Entity包含員工資料夾
![](http://Files.Dotblogs.com.tw/pbnttttt/0911/20091114192251524.png)
在Models資料夾下再建立一個Employees的Repository類別,名為EmployeeRepository,其中包含取得所有員工列表與員工圖片的方法.
02 | using System.Collections.Generic; |
06 | namespace ShowImageSample.Models |
08 | public class EmployeeRepository |
10 | private NorthwindEntities _entity; |
11 | public EmployeeRepository() |
13 | _entity = new NorthwindEntities(); |
16 | public IQueryable<Employees> GetEmpList() |
18 | return _entity.Employees; |
21 | public byte [] GetEmployeePhotoByID( int empid) |
23 | return _entity.Employees.First(x => x.EmployeeID == empid).Photo; |
4.建立一個Controller,名為Employee
02 | using System.Collections.Generic; |
06 | using System.Web.Mvc.Ajax; |
08 | using ShowImageSample.Helper; |
09 | using ShowImageSample.Models; |
11 | namespace ShowImageSample.Controllers |
13 | public class EmployeeController : Controller |
18 | public ActionResult Index() |
20 | EmployeeRepository emp = new EmployeeRepository(); |
21 | return View(emp.GetEmpList()); |
25 | public ActionResult EmployeePhoto( int id) |
27 | EmployeeRepository emp = new EmployeeRepository(); |
28 | byte [] empPhoto = emp.GetEmployeePhotoByID(id).Skip(78).ToArray(); |
29 | MemoryStream ms = new MemoryStream(empPhoto); |
30 | return new ImageResult(ms, "image/jpeg" ); |
5.在Index action上右鍵點選Add View
![](http://Files.Dotblogs.com.tw/pbnttttt/0911/20091114193947215.png)
6.這時會自動建立一個強型別的View,修改一下他的html,只顯示員工編號,名字,照片三個Column
13 | <% foreach (var item in Model) { %> |
16 | <%= Html.Encode(item.EmployeeID) %> |
19 | <%= Html.Encode(item.FirstName + " " + item.LastName) %> |
22 | <img alt= "photo" src= '<%= string.Format("Employee/EmployeePhoto/{0}", item.EmployeeID)%>' /> |
圖片的路徑指定的是 EmployeePhoto action
這時網頁會呈現如下:
![](http://Files.Dotblogs.com.tw/pbnttttt/0911/2009111419586242.png)
範例下載
![](http://files.dotblogs.com.tw/pbnttttt/0910/200910202119234.gif)
引用地址:http://www.dotblogs.com.tw/pbnttttt/archive/2009/11/14/11991.aspx?fid=11665#feedback