一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net文件断点续传代码

时间:2022-06-25 05:35:38 编辑:袖梨 来源:一聚教程网

asp教程.net文件断点续传代码

ftpwebrequest ftprequest = (ftpwebrequest)webrequest.create(_uri);
ftprequest.method = webrequestmethods.ftp.downloadfile;
ftprequest.keepalive = false;
ftprequest.usebinary = true;
ftprequest.contentoffset = segment.begin;
ftprequest.credentials = new networkcredential(_username, _password);
using (ftpwebresponse ftpresponse = (ftpwebresponse)ftprequest.getresponse())
using (filestream file = new filestream(savetemppath, filemode.open, fileaccess.write, fileshare.write))
using (stream responsestream = ftpresponse.getresponsestream())
{
 file.seek(segment.begin, seekorigin.begin);
 int poswrite = segment.begin;
 byte[] buf = new byte[_bufsize];
 int readbytes = 0;
 while (!responsestream.canread)
 {
  thread.sleep(0);
 }
 while ((readbytes = responsestream.read(buf, 0, buf.length)) > 0)
 {
  int byteslack = segment.end - poswrite + 1;
  if (readbytes > byteslack)
  {
   file.write(buf, 0, byteslack);
   poswrite += byteslack;
  }
  else
  {
   file.write(buf, 0, buf.length);
   poswrite += buf.length;
  }

  file.flush();
  segment.begin = poswrite;
  // todo: save segment

  if (poswrite > segment.end)
  {
   break;
  }
  while (!responsestream.canread)
  {
   thread.sleep(0);
  }
 }
 responsestream.close();
 file.close();
 ftpresponse.close();

//注明必须置contentoffset

热门栏目