怎样在asp.net中用一般处理文件ashx实现下载功能

怎样在asp.net中用一般处理文件ashx实现下载功能,第1张

01.<%@ WebHandler Language="C#" Class="download" %>

02.using System

03.using System.Web

04.public class download : IHttpHandler {

05.

06.public void ProcessRequest (HttpContext context) {

07.string url = HttpContext.Current.Server.UrlDecode(context.Request.QueryString["url"])

08.downloadfile(url)

09.}

10.

11.public bool IsReusable {

12.get {

13.return false

14.}

15.}

16.public void downloadfile(string s_fileName)

17.{

18. HttpContext.Current.Response.ContentType = "application/ms-download"

19. string s_path = HttpContext.Current.Server.MapPath("~/") + s_fileName

20. System.IO.FileInfo file = new System.IO.FileInfo(s_path)

21. HttpContext.Current.Response.Clear()

22. HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream")

23. HttpContext.Current.Response.Charset = "utf-8"

24. HttpContext.Current.Response.AddHeader("Content-Disposition", "attachmentfilename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8))

25. HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString())

26. HttpContext.Current.Response.WriteFile(file.FullName)

27. HttpContext.Current.Response.Flush()

28. HttpContext.Current.Response.Clear()

29. HttpContext.Current.Response.End()

30.}

31.}

Handler.ashx ? 引自网络... ----------- ASP.NET2.0 Person Web Site:将图片以二进制的从数据库读取 在ASP.NET Person Web Site Starter Kits中,图片的是以二进制的形式存放在数据库里,微软提供的读取该图片的方式是通过handler.ashx来实现的. 首先你所看到的对图片的引用都是类似如下 <img src="Handler.ashx?AlbumID=<%# Eval(" AlbumID")= %=>的形式,也就是你所看到的每一个图片都由Handler.ashx动态生成,所以在你保存你所看到的"图片"时,它的命名都是Handler.ashx. 使用handler.ashx而不是handler.aspx来引用图片并不是一定的,换句话说,你可以将该文件改为handler.aspx并没有本质的区别,不过根据微软的文档,使用*.ashx作为扩展名的文档比*.aspx的文档性能高,因为它减少了控件树的生成. ---------- 这是handler.ashx代码: <%@ WebHandler Language="C#" Class="MyHandle" %>using Systemusing System.Webusing System.Drawingusing System.Drawing.Imagingusing System.IOusing System.Web.Cachingusing System.Datapublic class MyHandle : IHttpHandler { public void ProcessRequest (HttpContext context) { //从数据库中取 MemoryStream ms = JSZ.SQLServerDAL.TestDAL.GetImage(1)DataTable dt = JSZ.SQLServerDAL.TestDAL.GetTable(1)Byte[] image = (Byte[])dt.Rows[0]["Photo"]MemoryStream ms = new MemoryStream(image, 0, image.Length)if (ms != null) { //取得图像MemoryStream大小 int bufferSize = (int)ms.Length//建立 buffer byte[] buffer = new byte[bufferSize]//调用MemoryStream.Read,自MemoryStream 读取至buffer,并返回count int countSize = ms.Read(buffer, 0, bufferSize)//返回图像buffer context.Response.OutputStream.Write(buffer, 0, countSize)} } public bool IsReusable { get { return false} } }

一般处理文件(ASHX)是可以通过前台调用的,你可以使用如: <img src="aaa.ashx?width=100&&Height=200" />这种方试直接调用这个返回图片的ASHX文件。

ASHX文件如下:

// 这个方法就是ASHX文件的入口点,建议在这里进行参数处理

public void ProcessRequest(HttpContext context)

{

//定议图片大小

int widthtmp = 50

int Heighttmp = 50

if (context.Request.QueryString["width"] != null &&!string.IsNullOrEmpty(context.Request.QueryString["width"].ToString()))

{

widthtmp = Convert.ToInt32(context.Request.QueryString["width"])

}

if (context.Request.QueryString["Height"] != null &&!string.IsNullOrEmpty(context.Request.QueryString["Height"].ToString()))

{

Heighttmp = Convert.ToInt32(context.Request.QueryString["width"])

}

//调用返图片方法并将返回的二进制写入HTTP输出流

context.Response.BinaryWrite(getimg(widthtmp, Heighttmp))

}

private byte[] getimg(int widthtmp,int Heighttmp)

{

//方法体记自定义

//getimg方法就是你说的返回一张图片的方法了,不过这个方法最后记得将图片转成二进制进行返回,

}

此外ASHX文件还可被JS调用和后台调用``通过JS加ASHX文件用来进行数剧访问就是人们所说的异步了。

如有兴趣可以直接找我``我可以传一些资料给你看


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11703362.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存