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

首頁 > 開發 > 綜合 > 正文

C#實現大文件分塊發送到客戶端

2024-07-21 02:26:31
字體:
來源:轉載
供稿:網友

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();
  }
 }
 

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南澳县| 淮阳县| 边坝县| 常山县| 庆阳市| 郸城县| 桓台县| 通江县| 塔城市| 仙桃市| 蒲江县| 六枝特区| 炉霍县| 蒲城县| 营山县| 虞城县| 海门市| 宝清县| 大方县| 东港市| 精河县| 新乡市| 大名县| 佛冈县| 亚东县| 社会| 大余县| 孟州市| 宁海县| 城市| 准格尔旗| 泗水县| 东乡县| 德州市| 北票市| 宣恩县| 合江县| 望谟县| 阳东县| 从化市| 章丘市|