• Asp.net文件下载


    多线程断点续传

     1         public bool ResponseDownload(string path) {
     2             long fileLength;
     3             long startRange = 0;
     4             int pack = 10240;//10k
     5             string filename=Path.GetFileName(path);
     6             try {
     7                 FileStream dStream = new FileStream(path, FileMode.Open, FileAccess.Read);
     8                 byte[] buffer = new byte[pack];
     9                 //BinaryReader br = new BinaryReader(dStream);
    10                 try {
    11                     Page.Response.Clear();
    12                     Response.Buffer = false;
    13                     Response.AddHeader("Accept-Ranges""bytes");
    14                     
    15                     fileLength = dStream.Length;
    16                     long sp = 0;
    17                     long ep = 0;
    18                     if (Request.Headers["Range"!= null) {
    19                         Response.StatusCode = 206;
    20                         string[] range = Request.Headers["Range"].Split(new char[] { '=''-' });
    21                         if (range.Length == 3&&!string.IsNullOrEmpty(range[2])) {
    22                             ep = Convert.ToInt64(range[2]);
    23                         }
    24                         sp = Convert.ToInt64(range[1]);
    25                         if (ep < sp) {
    26                             ep = 0;
    27                         }
    28                     }
    29                     long contLength = ep == 0 ? fileLength - sp : ep - sp + 1;
    30                     
    31                     if (sp != 0) {
    32                         Response.AddHeader("Content-Range", String.Format("bytes {0}-{1}/{2}", sp, ep == 0 ? fileLength - 1 : ep, fileLength));
    33                     }
    34 
    35                     Response.AddHeader("Content-Length", contLength.ToString());
    36                     Response.AddHeader("Content-Disposition""attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
    37                     Response.AddHeader("Connection""Keep-Alive");
    38                     Response.ContentType = "application/octest-stream";
    39                     Response.Flush();
    40                     
    41                     //calculate file size;
    42                     long maxCount = ep==0?fileLength:ep+1;
    43 
    44                     maxCount = maxCount - sp;
    45 
    46                     //move file handler to range point.
    47                     dStream.Seek(sp, SeekOrigin.Begin);
    48 
    49                     while (maxCount > 0&&Response.IsClientConnected) {
    50                         int lengthRead = dStream.Read(buffer, 0, Convert.ToInt32(pack));
    51                         Response.OutputStream.Write(buffer, 0, lengthRead);
    52                         Response.Flush();    
    53                         maxCount -= pack;
    54                     }
    55                     
    56                 }
    57                 catch (Exception e) {
    58                     Response.Write("<h3>Error:</h3>" + e.Message);
    59                     return false;
    60                 }
    61                 finally {
    62                     dStream.Close();
    63                 }
    64 
    65             }
    66             catch (Exception ex) {
    67                 Response.Write("<h3>Error:</h3>" + ex.Message);
    68                 return false;
    69             }
    70             return true;
    71         }
    披荆斩棘,朝自己的信仰!Go on
  • 相关阅读:
    React——from
    React——条件渲染
    React——event
    React——组件
    React——JSX
    flask_mail使用
    flask开发restful api
    mysql limit和offset用法
    flask打包安装文件
    flask-sqlalchemy使用及数据迁移
  • 原文地址:https://www.cnblogs.com/lyk831216/p/1743391.html
Copyright © 2020-2023  润新知