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     }
  • 相关阅读:
    Android WebView重定向问题的解决方案
    Android 控件背景选择图片还是drawable XML资源
    Android AlertDialog 绝对位置计算
    Android 5.0以上Material Design 沉浸式状态栏
    Android 6.0系统动态请求系统相机权限
    Android软键盘在清单文件中所有配置含义
    Android,TextView的所有属性和方法
    【转载】Android控件属性大全
    Android布局及控件的属性说明
    android带有文字的图片按钮的两种实现方式
  • 原文地址:https://www.cnblogs.com/skyshenwei/p/1705026.html
Copyright © 2011-2022 走看看