使用一般处理程序.ashx

一:访问

访问时要带后缀例如:http://localhost:15343/updateAPK.ashx 



二下载文件,例如作用更新apk的后台

[csharp]  view plain  copy
  1. public class updateAPK : IHttpHandler  
  2.    {  
  3.   
  4.        public void ProcessRequest(HttpContext context)  
  5.        {  
  6.            HttpResponse Response = context.Response;  
  7.            HttpRequest Request = context.Request;  
  8.   
  9.            System.IO.Stream iStream = null;  
  10.            byte[] buffer = new Byte[10240];  
  11.            int length;  
  12.            long dataToRead;  
  13.            try  
  14.            {  
  15.                //获取虚拟目录的物理路径。  
  16.                string filepath = context.Server.MapPath("Content/Ship.apk");//待下载的文件路径  
  17.                iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,  
  18.                    System.IO.FileAccess.Read, System.IO.FileShare.Read);  
  19.                Response.Clear();  
  20.                dataToRead = iStream.Length;  
  21.                long p = 0;  
  22.                if (Request.Headers["Range"] != null)  
  23.                {  
  24.                    Response.StatusCode = 206;  
  25.                    p = long.Parse(Request.Headers["Range"].Replace("bytes=""").Replace("-"""));  
  26.                }  
  27.                if (p != 0)  
  28.                {  
  29.                    Response.AddHeader("Content-Range""bytes " + p.ToString() + "-" + ((long)(dataToRead - 1)).ToString() + "/" + dataToRead.ToString());  
  30.                }  
  31.                Response.AddHeader("Content-Length", ((long)(dataToRead - p)).ToString());  
  32.                Response.ContentType = "application/octet-stream";  
  33.                Response.AddHeader("Content-Disposition""attachment; filename=" + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.GetEncoding(65001).GetBytes(System.IO.Path.GetFileName("Ship.apk"))));  
  34.                iStream.Position = p;  
  35.                dataToRead = dataToRead - p;  
  36.                while (dataToRead > 0)  
  37.                {  
  38.                    if (Response.IsClientConnected)  
  39.                    {  
  40.                        length = iStream.Read(buffer, 0, 10240);  
  41.                        Response.OutputStream.Write(buffer, 0, length);  
  42.                        Response.Flush();  
  43.                        buffer = new Byte[10240];  
  44.                        dataToRead = dataToRead - length;  
  45.                    }  
  46.                    else  
  47.                    {  
  48.                        dataToRead = -1;  
  49.                    }  
  50.                }  
  51.            }  
  52.            catch (Exception ex)  
  53.            {  
  54.                Response.Write("Error : " + ex.Message);  
  55.            }  
  56.            finally  
  57.            {  
  58.                if (iStream != null)  
  59.                {  
  60.                    iStream.Close();  
  61.                }  
  62.                Response.End();  
  63.            }  
  64.        }  
  65.   
  66.        public bool IsReusable  
  67.        {  
  68.            get  
  69.            {  
  70.                return false;  
  71.            }  
  72.        }  
  73.    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值