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

来源:互联网 发布:php method 编辑:程序博客网 时间:2024/05/29 18:35
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
.Vvy493{display:none;}

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

例程:

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

关于ContentType(MIMEType)的参考URL:

http://www.w3.org/TR/html4/types.html(概述)

http://www.iana.org/assignments/media-types/(详细列表)

常见问题解决方案:

1、当从资源文件或者数据库BLOB字段载入图像出现错误

错误内容:AgenericerroroccurredinGDI+

代码:


Bitmapbmp=this.GetGlobalResourceObject(“Resource”,”_BitMap”)asBitmap;

Response.ContentType=”image/jpeg”;
bmp.Save(Response.OutputStream,ImageFormat.Jpeg);

Response.End();

解决方法,再创建一个实例接收从资源文件或者数据库BLOB字段读入的图像内容。

解决方案代码:


Bitmapbmp=this.GetGlobalResourceObject(“Resource”,”_BitMap”)asBitmap;
Bitmaptemp=newBitmap(bmp);

Response.ContentType=“image/jpeg”;
Temp.Save(Response.OutputStream,ImageFormat.Jpeg);

bmp.Dispose();
temp.Dispose();

Response.End();

2、无法直接把PNG图像存入到输出流

原因:PNG是特殊的图片格式

解决方案代码:


Bitmapbmp=this.GetGlobalResourceObject(“Resource”,“_BitMap”)asBitmap;
Bitmaptemp=newBitmap(bmp);

MemoryStreamms=newMemoryStream();

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

MemoryStreamstream1=newMemoryStream();

//DrawPiemethodreturnanImage
This.DrawPie(table1).Save(stream1,ImageFormat.Png);
Response.BinaryWrite(stream1.ToArray());

Base.OnPreInit(e);

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击