ASP.NET中通过对话框方式下载文件
发布时间:2013-09-19 11:33:46来源:阅读(1500)
ASP.NET中通过对话框方式下载文件
1 通过探出对话框提示文件下载或打开
[中国站长站]
2 通过自定义Header让特定的应用程序打开文件 Chinaz.com
使用的方法:Response.TransmitFile() Chinaz.com
例程:
[中国站长站]
Response.ContentType = “image/jpeg”;
Response.AppendHeader(“Content-Disposition”,”attachment; filename=SailBig.jpg”);
Response.TransmitFile( Server.MapPath(“~/images/sailbig.jpg”) ); [中国站长站]
流传送所使用的方法:Response.BinaryWrite()和Response.OutputStream()
Chinaz.com
例程: Chinaz.com
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 (概述) Chinaz.com
http://www.iana.org/assignments/media-types/ (详细列表) [中国站长站]
常见问题解决方案:
Chinaz.com
1、当从资源文件或者数据库BLOB字段载入图像出现错误
[中国站长站]
错误内容:A generic error occurred in GDI+
Chinaz.com
代码:
[中国站长站]
以下为引用的内容:
Bitmap bmp = this.GetGlobalResourceObject(“Resource”,”_BitMap”) as Bitmap;
[中国站长站]
Response.ContentType=”image/jpeg”;
bmp.Save(Response.OutputStream, ImageFormat.Jpeg); Chinaz.com
Response.End(); Chinaz.com
[中国站长站]
解决方法,再创建一个实例接收从资源文件或者数据库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(); Chinaz.com
Response.End();
[中国站长站]
Chinaz.com
2、无法直接把PNG图像存入到输出流
Chinaz.com
原因:PNG是特殊的图片格式
Chinaz.com
解决方案代码:
[中国站长站]
以下为引用的内容:
Bitmap bmp = this.GetGlobalResourceObject( “Resource”, “_BitMap”) as Bitmap;
Bitmap temp = new Bitmap(bmp); Chinaz.com
MemoryStream ms = new MemoryStream();
[中国站长站]
Response.ContentType=”image/png”;
temp.Save(ms, System.Drawing.Imaging, ImageFormat.Png);
Ms.WriteTo(Response.OutputStream);
[中国站长站]
bmp.Dispose();
temp.Dispose();
Chinaz.com
Response.End(); Chinaz.com [中国站长站]
3、解决缓存问题
Chinaz.com
以下为引用的内容:
Response.ContentType=”image/png”;
Response.Buffer = false;
Response.Clear(); Chinaz.com
MemoryStream stream1 = new MemoryStream();
[中国站长站]
// DrawPie method return an Image
This.DrawPie(table1).Save(stream1,ImageFormat.Png);
Response.BinaryWrite(stream1.ToArray()); [中国站长站]
Base.OnPreInit(e);
Chinaz.com