zoukankan      html  css  js  c++  java
  • aspx.net 页面下文件下载功能函数整理

    1 public void FileDownLoadDel(string fullFilename)
     2     {
     3         System.IO.Stream iStream = null;
     4         // Buffer to read 10K bytes in chunk:
     5         byte[] buffer = new Byte[10000];
     6 
     7         // Length of the file:
     8         int length;
     9 
    10         // Total bytes to read:
    11         long dataToRead;
    12 
    13         // Identify the file to download including its path.
    14         string filepath = fullFilename;
    15         filepath = Server.MapPath(filepath);
    16 
    17         // Identify the file name.
    18         string filename = System.IO.Path.GetFileName(filepath);
    19 
    20 
    21         try
    22         {
    23             // Open the file.
    24             iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
    25                         System.IO.FileAccess.Read, System.IO.FileShare.Read);
    26 
    27 
    28             // Total bytes to read:
    29             dataToRead = iStream.Length;
    30 
    31             Response.ContentType = "application/octet-stream";
    32             Response.AddHeader("Content-Disposition""attachment; filename=" + filename);
    33 
    34             // Read the bytes.
    35             while (dataToRead > 0)
    36             {
    37                 // Verify that the client is connected.
    38                 if (Response.IsClientConnected)
    39                 {
    40                     // Read the data in buffer.
    41                     length = iStream.Read(buffer, 010000);
    42 
    43                     // Write the data to the current output stream.
    44                     Response.OutputStream.Write(buffer, 0, length);
    45 
    46                     // Flush the data to the HTML output.
    47                     Response.Flush();
    48 
    49                     buffer = new Byte[10000];
    50                     dataToRead = dataToRead - length;
    51                 }
    52                 else
    53                 {
    54                     //prevent infinite loop if user disconnects
    55                     dataToRead = -1;
    56                     Response.Clear();
    57                 }
    58             }
    59             Response.End(); //没有这句会将该页面刷新后的内容追加写入文件中。
    60         }
    61         catch (Exception ex)
    62         {
    63             // Trap the error, if any.
    64             //Response.Write("Error : " + ex.Message);
    65             //base.WriteLog("资料", "下载资料:" + ex.Message + "!", LogType.Error, this.GetType().ToString());
    66 
    67         }
    68         finally
    69         {
    70             if (iStream != null)
    71             {
    72                 //Close the file.
    73                 iStream.Close();
    74             }
    75             File.Delete(fullFilename);
    76         }
    77     }
  • 相关阅读:
    【树论】新二叉树
    【贪心】umi的函数
    【模拟】高精度练习之加法
    【模拟】高精度练习之减法
    【贪心】线段覆盖
    【树论】二叉树的序遍历
    【贪心】超级书架
    【动态规划】爱与愁的心痛
    【图论】牛大赛
    【图论】产生数
  • 原文地址:https://www.cnblogs.com/skyshenwei/p/1705026.html
Copyright © 2011-2022 走看看