ASP.NET中通過對話框方式下載文件
發表于:2007-09-07來源:作者:點擊數:
標簽:
ASP.NET中通過對話框方式下載文件 1 通過探出對話框提示文件下載或打開 2 通過自定義Header讓特定的應用程序打開文件 使用的方法:Response.TransmitFile() 例程: Response.ContentType = “image/jpeg”; Response.AppendHeader(“Content-Disposition”,
ASP.NET中通過對話框方式
下載文件
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 o
clearcase/" target="_blank" >ccurred 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);
可以參考的資料:
Build Smarter ASP.NET File Downloading Into Your Web Applications
http://msdn.microsoft.com/msdnmag/issues/06/09/webdownloads/default.aspx
How to: Download file from ASP.Net 2.0 Page
http://geekswithblogs.net/rachit/archive/2007/04/20/How-to-ASP.Net-File-Download.aspx

【責任編輯 彭凡】
|
原文轉自:http://www.anti-gravitydesign.com