ASP.NET中通過對話方塊方式下載檔案

來源:互聯網
上載者:User

 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);
 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.