MSSQL中的image資料型別是 0 ~ 2^31-1 (2,147,483,647) bytes的binary data,我懶得弄一個DB去拉資料,所以拉了之前去西門町吃排骨酥湯時拍的照片來當Source,然後設好網頁的ContentType後吐出來。
語法如下,我是寫在ASP.NET MVC下,不過基本上沒差啦。
public class YooooController : Controller { public void Img() { System.Net.WebClient wc = new System.Net.WebClient(); byte[] data = wc.DownloadData("http://farm6.static.flickr.com/5010/5352949971_1938f31b9e.jpg"); Response.ContentType = "Image/jpg"; Response.OutputStream.Write(data, 0, data.Length); Response.OutputStream.Flush(); Response.End(); } }
這是在Yoooo Controller下的Img Action,所以要讀這張圖的時候就用一般的img標籤,只是src指到這個Action。
@{ ViewBag.Title = "Home Page"; } <h2>@ViewBag.Message</h2> <p> To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>. </p> <img src="/yoooo/img" />
最後的結果就直接跑出一張圖啦
No comments:
Post a Comment