首页 > 编程 > .NET > 正文

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

2024-07-10 13:07:44
字体:
来源:转载
供稿:网友

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+

代码:

bitmap bmp = this.getglobalresourceobject(“resource”,”_bitmap”) as bitmap;
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();
 
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);

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表