1 通過探出對話方塊提示檔案下載或開啟
2 通過自訂Header讓特定的應用程式開啟檔案
使用的方法:Response.TransmitFile()
常式:
Response.ContentType = “image/jpeg”;
Response.AppendHeader(“Content-Disposition”,”attachment; filename=SailBig.jpg”);
Response.TransmitFile( Server.MapPath(“~/images/sailbig.jpg”) );
流傳送所使用的方法:
Response.BinaryWrite()和Response.OutputStream()
常式:
Bitmap bmp = wwWebUtils.CornerImage(backcolor, color, c, Radius, Height, Width);
Response.ContentType = “image/jpeg”;
Response.AppendHeader(“Content-Disposition”,”attenment; filename=LeftCorner.jpg”);
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
關於Content Type(MIME Type)的參考URL:
http://www.w3.org/TR/html4/types.html (概述)
http://www.iana.org/assignments/media-types/ (詳細列表)
常見問題解決方案:
1、當從資源檔或者資料庫BLOB欄位載入映像出現錯誤
錯誤內容:A generic error occurred in GDI+
代碼:
Bitmap bmp = this.GetGlobalResourceObject(“Resource”,”_BitMap”) as Bitmap;
Response.ContentType=”image/jpeg”;
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
Response.End();
解決方案,再建立一個執行個體接收從資源檔或者資料庫BLOB欄位讀入的映像內容。
解決方案代碼:
Bitmap bmp = this.GetGlobalResourceObject(“Resource”, ”_BitMap”) as Bitmap;
Bitmap temp = new Bitmap(bmp);
Response.ContentType = “image/jpeg”;
Temp.Save(Response.OutputStream, ImageFormat.Jpeg);
bmp.Dispose();
temp.Dispose();
Response.End();
2、無法直接把PNG映像存入到輸出資料流
原因:PNG是特殊的圖片格式
解決方案代碼:
Bitmap bmp = this.GetGlobalResourceObject( “Resource”, “_BitMap”) as Bitmap;
Bitmap temp = new Bitmap(bmp);
MemoryStream ms = new MemoryStream();
Response.ContentType=”image/png”;
temp.Save(ms, System.Drawing.Imaging, ImageFormat.Png);
Ms.WriteTo(Response.OutputStream);
bmp.Dispose();
temp.Dispose();
Response.End();
3、解決緩衝問題
Response.ContentType=”image/png”;
Response.Buffer = false;
Response.Clear();
MemoryStream stream1 = new MemoryStream();
// DrawPie method return an Image
This.DrawPie(table1).Save(stream1,ImageFormat.Png);
Response.BinaryWrite(stream1.ToArray());
Base.OnPreInit(e);