Welcome 微信登录

首页 / 网页编程 / ASP.NET / ASP.NET中通过对话框方式下载文件

ASP.NET中通过对话框方式下载文件2011-02-241 通过探出对话框提示文件下载或打开

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

例程:

以下是引用片段:
Bitmapbmp=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+

代码:

以下是引用片段:
Bitmapbmp=this.GetGlobalResourceObject(“Resource”,”_BitMap”)asBitmap;
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();