使用一般处理程序.ashx
来源:互联网 发布:king最新域名 编辑:程序博客网 时间:2024/05/20 01:38
一:访问
访问时要带后缀例如:http://localhost:15343/updateAPK.ashx
二下载文件,例如作用更新apk的后台
public class updateAPK : IHttpHandler { public void ProcessRequest(HttpContext context) { HttpResponse Response = context.Response; HttpRequest Request = context.Request; System.IO.Stream iStream = null; byte[] buffer = new Byte[10240]; int length; long dataToRead; try { //获取虚拟目录的物理路径。 string filepath = context.Server.MapPath("Content/Ship.apk");//待下载的文件路径 iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); Response.Clear(); dataToRead = iStream.Length; long p = 0; if (Request.Headers["Range"] != null) { Response.StatusCode = 206; p = long.Parse(Request.Headers["Range"].Replace("bytes=", "").Replace("-", "")); } if (p != 0) { Response.AddHeader("Content-Range", "bytes " + p.ToString() + "-" + ((long)(dataToRead - 1)).ToString() + "/" + dataToRead.ToString()); } Response.AddHeader("Content-Length", ((long)(dataToRead - p)).ToString()); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.GetEncoding(65001).GetBytes(System.IO.Path.GetFileName("Ship.apk")))); iStream.Position = p; dataToRead = dataToRead - p; while (dataToRead > 0) { if (Response.IsClientConnected) { length = iStream.Read(buffer, 0, 10240); Response.OutputStream.Write(buffer, 0, length); Response.Flush(); buffer = new Byte[10240]; dataToRead = dataToRead - length; } else { dataToRead = -1; } } } catch (Exception ex) { Response.Write("Error : " + ex.Message); } finally { if (iStream != null) { iStream.Close(); } Response.End(); } } public bool IsReusable { get { return false; } } }
0 0
- 使用一般处理程序.ashx
- 使用一般处理程序.ashx
- 一般处理程序.ashx
- ASHX 一般处理程序
- ashx一般处理程序
- ashx一般处理程序
- C#创建使用一般处理程序.ashx
- asp.net .ashx一般处理程序中使用session
- .NET中一般处理程序(ashx)在Ajax中的使用
- asp.net .ashx一般处理程序中使用Session
- .NET中一般处理程序(ashx)在Ajax中的使用
- aspx,ascx和ashx(一般处理程序)使用小结
- 一般处理程序(.ashx)中使用Session (转)
- 在ashx一般处理程序中使用session
- .NET中一般处理程序(ashx)的使用
- 使用ashx一般处理程序,读取不到Session的问题
- .net使用一般处理程序matermark.ashx代码
- ashx一般处理程序Session处理
- CentOS6.5分区与文件系统
- httpurlconnection
- 《大话设计模式》--无熟人难办事?--迪米特法则<Lod>(11)
- 通过反射获取实体的一切属性
- DevC++中使用boost简析
- 使用一般处理程序.ashx
- 1028. List Sorting (25)
- VMWare克隆或复制Linux虚拟机后无法上网的解决
- systemtap和火焰图(flamegraph)介绍
- 卸载ubuntu,保留windows
- Quartz创建多个不同名字的scheduler实例
- 关闭UITableViewCell选中效果
- 马云的儿子马元坤
- 毁掉程序员健康的5件事,你中了几招?