怎么从服务器下载大文件?(在线等)
本人在Win Form编程中,想通过Web Service从服务器从下载大小为几百M的文件.我现在只能实现大小为几十M的文件的下载.请高人指教.
------解决方案--------------------/// <summary>
/// 下载升级文件
/// </summary>
/// <param name= "FileName "> 文件名称 </param>
/// <param name= "ByteOffset "> 偏移地址 </param>
/// <param name= "ByteLength "> 下载长度 </param>
/// <returns> 编码字符 </returns>
[WebMethod(EnableSession = true, Description = "下载升级文件 ")]
public string DownloadByteFile(string FileName, long ByteOffset, long ByteLength)
{
try
{
string FilePath = Server.MapPath(DownLoadPath) + "\\ " + FileName;
FileStream fstream = new FileStream(FilePath, FileMode.Open);
byte[] farray = new byte[ByteLength];
fstream.Position = ByteOffset;
fstream.Read(farray, 0, (int)ByteLength);
fstream.Close();
return Convert.ToBase64String(farray);
}
catch (Exception)
{
return string.Empty;
}
}
------解决方案--------------------string fileString = service.DownloadByteFile(dinfo.DownLoadFileName, dinfo.DownLength, dinfo.ByteLength);
if (fileString != string.Empty)
{
MemoryStream mstream = new MemoryStream(Convert.FromBase64String(fileString));
byte[] farray = new byte[mstream.Length];
mstream.Read(farray, 0, (int)mstream.Length);
mstream.Close();
FileStream fstream = new FileStream(dinfo.DownLoadFileName, FileMode.Append);
fstream.Write(farray, 0, farray.Length);
fstream.Flush();
fstream.Close();
dinfo.DownLength = dinfo.DownLength + farray.Length;
dinfo.SaveToXMLFile(FileName);
this.progress.Value = (int)(dinfo.DownLength <= dinfo.FileLength ? dinfo.DownLength : dinfo.FileLength);
this.lblWarning.Text = "下载升级文件 - " + dinfo.DownLoadFileName + "[ " + dinfo.DownLength + "/ " + dinfo.FileLength + "] ";
this.lblWarning.Refresh();
if (dinfo.DownLength > = dinfo.FileLength)
{
this.btnOK.Text = "完成(&F) ";
break;
}
}
------解决方案--------------------to 我用的方法和你的几乎一样,不同的是:我retrurn 的是 一个byte[] .
用byte[]没有什么问题,你可能是其他地方出的问题。
------解决方案--------------------byte[] b = new byte[fs.Length];
天那 如果这个文件很大很大岂不是。。。
web服务里面只需要提供一个下载地址就行了
客户端采用断点续传的方式一个缓存一个缓存的下载