国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

C#實(shí)現(xiàn)大文件分塊發(fā)送到客戶端

2019-11-18 19:29:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

System.IO.Stream iStream = null;

 // Buffer to read 10K bytes in chunk:
 byte[] buffer = new Byte[10000];

 // Length of the file:
 int length;

 // Total bytes to read:
 long dataToRead;

 // Identify the file to download including its path.
 string filepath  = "DownloadFileName";

 // Identify the file name.
 string  filename  = System.IO.Path.GetFileName(filepath);

 try
 {
  // Open the file.
  iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
     System.IO.Fileaccess.Read,System.IO.FileShare.Read);


  // Total bytes to read:
  dataToRead = iStream.Length;

  Response.ContentType = "application/octet-stream";
  Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

  // Read the bytes.
    while (dataToRead > 0)
  {
   // Verify that the client is connected.
   if (Response.IsClientConnected)
   {
    // Read the data in buffer.
    length = iStream.Read(buffer, 0, 10000);

    // Write the data to the current output stream.
    Response.OutputStream.Write(buffer, 0, length);

    // Flush the data to the HTML output.
    Response.Flush();

    buffer= new Byte[10000];
    dataToRead = dataToRead - length;
   }
   else
   {
    //PRevent infinite loop if user disconnects
    dataToRead = -1;
   }
  }
 }
 catch (Exception ex)
 {
  // Trap the error, if any.
  Response.Write("Error : " + ex.Message);
 }
 finally
 {
  if (iStream != null)
  {
   //Close the file.
   iStream.Close();
  }
 }

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永平县| 泰顺县| 马尔康县| 东阳市| 吉木乃县| 仙游县| 西贡区| 朝阳县| 铜陵市| 那坡县| 通许县| 依兰县| 温州市| 利川市| 政和县| 阿瓦提县| 台南市| 武宣县| 开封市| 多伦县| 青川县| 临安市| 平昌县| 邯郸县| 淮阳县| 平昌县| 资溪县| 宕昌县| 额济纳旗| 辰溪县| 新建县| 汉阴县| 尼木县| 平乐县| 大名县| 楚雄市| 曲靖市| 共和县| 滕州市| 衡阳市| 富锦市|