当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET中通过对话框方式下载文件

ASP.NET
VS 2008和.NET 3.5 Beta2新特性介绍
VS 2008和.NET 3.5 Beta2常见问题的解决方案
Asp.net 备份和还原SQL Server及压缩Access数据库
Asp.Net中动态页面转静态页面
ASP.NET缓存:方法分析和实践示例
ASP.NET Forms验证(自定义、角色提供程序)
ASP.NET 2.0当中的Call Back机制
ASP.NET中MD5和SHA1加密的几种方法
在ASP.NET Atlas中调用Web Service
Cast的妙用:泛用LINQ 語句
ASP.NET将物件序列化成Binary储存至DB or File
使用Ajax后,原来导出功能失败的解决方法
装箱、转型、方法调用他们究竟有什么区别?
ASP.NET MVC :实现我们自己的视图引擎
如何构造一个C#语言的爬虫程序
Asp.net Mvc Framework可以在Controller中使用的Url.Action方法
校内网API的.net版本XiaoNei.Net 1.0(非官方)
使用ExtJS GridPanel从Web Service 获取、绑定和显示数据
从UI->DB一条龙到代码生成到EOS,谈谈快速开发
ASP.Net安装简明手册

ASP.NET中通过对话框方式下载文件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 170 ::
收藏到网摘: n/a

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 occurred in GDI+

代码:

解决方法,再创建一个实例接收从资源文件或者数据库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);