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

ASP.NET
.NET:InTheHand提供的类操作SMS
捕捉WPF应用程序中XAML代码解析异常
学习asp.net之SQL语句查询效率和安全性
一个不被flash、select、activex遮挡的、跨frame的无限分级菜单
探索 ASP.NET Futures
MSDN Visual系列:创建SharePoint Services解决方案包
.net中如何给数据表实体类赋值的方法
关于C#静态构造函数的几点说明
经典教程:用.net动态创建类的实例
ASP.NET 遍历配置文件的连接字符串
ASP.Net中利用CSS实现多界面两法
ASP.NET生成静态HTML页面并分别按年月目录存放
如何在ASP.net(C#)下操作XML文件
技巧应用:ASP.Net中的MD5加密
三层架构各层间的访问过程
asp.net初学者:petshop4.0设计说明
asp.net利用RAR实现文件压缩解压缩
.NET2.0抓取网页全部链接
给初学.net的您几点建议
如何实现ASP.NET网站个性化

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 152 ::
收藏到网摘: 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);