关于断点续传的问题,请 hdt(近来工作忙,每天只能接分半个小时)等高手再来

sywcf 2005-08-25 09:16:21
原贴:http://community.csdn.net/Expert/topic/4227/4228000.xml?temp=.8494074

这个程序好像不能断点续传,它是把整个文件全都重新下了一遍,当下第二次的时候,文件大小把第一次的也加上了,要怎样改一下。
...全文
396 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuanzhihua520 2006-07-02
  • 打赏
  • 举报
回复
up
风之浪漫 2006-02-14
  • 打赏
  • 举报
回复
关注一下 ~
givenchy 2005-08-27
  • 打赏
  • 举报
回复
好东西,收藏!
sywcf 2005-08-27
  • 打赏
  • 举报
回复
也感谢:longxin123(龙的心)兄弟,

不过你的方法我以前用过,只能下单个文件,而且要弹出对话框,
我是要下好多文件的,所以不行。
sywcf 2005-08-26
  • 打赏
  • 举报
回复
to :hdt(近来工作忙,每天只能接分半个小时)
为什么我下载时,int nSize = (int)hwrp.ContentLength;
nSize的值有时显示为:-1呢?

但实际上这个url是有内容的。
sywcf 2005-08-26
  • 打赏
  • 举报
回复
谢 hdt(近来工作忙,每天只能接分半个小时)

这个可以自动断点续传吗?

我这两天一直在学习处理流文件,现在知道下载的方法太多了,同时继续努力学习
sywcf 2005-08-26
  • 打赏
  • 举报
回复
谢 hdt(近来工作忙,每天只能接分半个小时)

这个可以自动断点续传吗?

我这两天一直在学习处理流文件,现在知道下载的方法太多了,同时继续努力学习
wdszya 2005-08-26
  • 打赏
  • 举报
回复
楼主的问题太深奥了
真相重于对错 2005-08-26
  • 打赏
  • 举报
回复
System.Net.HttpWebRequest hwrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://www.arms.org.cn/download/数据提交与上传.ppt");

System.Net.HttpWebResponse hwrp = (System.Net.HttpWebResponse)hwrq.GetResponse();
int nSize = (int)hwrp.ContentLength;
int nPosition =0;
int nLeftSize = nSize - nPosition;
byte[] bFile;
System.IO.FileStream fs = System.IO.File.Create("1.ppt");
while( nLeftSize > 0 )
{
int nRead = 0;
int nBlock = 0;
if( nLeftSize > 1024 )
{

hwrq.AddRange( nPosition , nPosition+1024 );

nBlock = 1024;
}
else
{
hwrq.AddRange( nPosition , nPosition+nLeftSize );
nBlock = nLeftSize;
}
hwrp = (System.Net.HttpWebResponse)hwrq.GetResponse();
System.IO.Stream stream = hwrp.GetResponseStream();
bFile = new byte[nBlock];
nRead = stream.Read( bFile , 0 , nBlock );
if( nRead > 0)
{
fs.Write( bFile , 0 , nRead );
System.Console.WriteLine( nLeftSize.ToString() );
nLeftSize -= nRead;
nPosition += nRead;
}
else
{
break;
}
}
fs.Close();System.Net.HttpWebRequest hwrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://www.arms.org.cn/download/数据提交与上传.ppt");

System.Net.HttpWebResponse hwrp = (System.Net.HttpWebResponse)hwrq.GetResponse();
int nSize = (int)hwrp.ContentLength;
int nPosition =0;
int nLeftSize = nSize - nPosition;
byte[] bFile;
System.IO.FileStream fs = System.IO.File.Create("1.ppt");
while( nLeftSize > 0 )
{
int nRead = 0;
int nBlock = 0;
if( nLeftSize > 1024 )
{

hwrq.AddRange( nPosition , nPosition+1024 );

nBlock = 1024;
}
else
{
hwrq.AddRange( nPosition , nPosition+nLeftSize );
nBlock = nLeftSize;
}
hwrp = (System.Net.HttpWebResponse)hwrq.GetResponse();
System.IO.Stream stream = hwrp.GetResponseStream();
bFile = new byte[nBlock];
nRead = stream.Read( bFile , 0 , nBlock );
if( nRead > 0)
{
fs.Write( bFile , 0 , nRead );
System.Console.WriteLine( nLeftSize.ToString() );
nLeftSize -= nRead;
nPosition += nRead;
}
else
{
break;
}
}
fs.Close();
lionelwy 2005-08-26
  • 打赏
  • 举报
回复
up
sywcf 2005-08-26
  • 打赏
  • 举报
回复
谢谢 mathsword(梦在流浪

我目前的程序用的就是:http://dev.21tx.com/2004/03/26/10458.html这个

至于那个多线程下载的程序,我也试过,根本就不好用啊,也不知为什么。

wuyi8808 2005-08-26
  • 打赏
  • 举报
回复
看看.
winterice 2005-08-26
  • 打赏
  • 举报
回复
学习...
longxin123 2005-08-26
  • 打赏
  • 举报
回复
我瞅瞅
adandelion 2005-08-26
  • 打赏
  • 举报
回复
up
mathsword 2005-08-26
  • 打赏
  • 举报
回复
我找的断点续传的东东,希望对你有帮助
http://dev.21tx.com/2004/03/26/10458.html
http://www.51zyz.com/118/show_15741.htm
http://www.xmlasp.net/n1445c13.aspx

暂时贴这几个,等有时间再贴,你的帖子等有时间帮你看看
daodaodaodao123 2005-08-26
  • 打赏
  • 举报
回复
收益非浅啊,顶
sywcf 2005-08-26
  • 打赏
  • 举报
回复
大家过来up一下,我好把分散了啊
longxin123 2005-08-26
  • 打赏
  • 举报
回复
System.IO.Stream iStream = null;

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

// Length of the file:
int length;

// Total bytes to read:
long dataToRead;

// Identify the file to download including its path.
string filepath = @"E:\software\SQL Server 2000 Personal Edition.ISO";

// 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);
Response.Clear();

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

long p = 0;
if(Request.Headers["Range"]!=null)
{
Response.StatusCode = 206;
p = long.Parse( Request.Headers["Range"].Replace("bytes=","").Replace("-",""));
}
if(p != 0)
{
Response.AddHeader("Content-Range","bytes " + p.ToString() + "-" + ((long)(dataToRead - 1)).ToString() + "/" + dataToRead.ToString());
}
Response.AddHeader("Content-Length",((long)(dataToRead-p)).ToString());
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(filename)));

iStream.Position = p;
dataToRead = dataToRead - p;
// 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, 10240);

// 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[10240];
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();
}
Response.End();
}
真相重于对错 2005-08-26
  • 打赏
  • 举报
回复
你要下的我看了,是通过https来连接,我用网络蚂蚁下,也无法得到他的大小,而且连接速度异常慢,这种方式我没搞过,估计你得在读取时检测大小了
加载更多回复(16)

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧