/// <summary>
/// 服務端:
/// </summary>
/// <param name="FileName">更新文件包名</param>
/// <param name="Offset">偏移</param>
/// <param name="Count">每次讀取字節數 單位 KB</param>
/// <returns>字節組</returns>
[WebMethod(Description = "<b>大文件下載</b> 測試")]
public byte[] getFile(string FileName, long Offset, int Count)
{
//指定下載文件夾+文件名
string strPath = @"E:\\" + FileName;
if (Offset < 0) { Offset = 0; }
byte[] btBuf = new byte[Count];
if (System.IO.File.Exists(strPath))
{
System.IO.FileStream fs = new System.IO.FileStream(strPath, System.IO.FileMode.Open);
if (Offset < fs.Length)
{
if (0 < (int)(fs.Length - Offset) && (int) (fs.Length - Offset) < Count)
{
Count = (int)(fs.Length - Offset);
}
btBuf=new byte[Count];
fs.Seek(Offset, System.IO.SeekOrigin.Begin);
fs.Read(btBuf, 0, Count);
}
else
{ btBuf = null; }
fs.Flush();
fs.Close();
fs.Dispose();
}
return btBuf;
}
//客戶端調用:
private string DownLoadPath(string strClientFilePath, string fileName)
{
string result = "DownLoading...";
ws1.Service s1 = new ws1.Service();
//本地數據
System.IO.FileStream ns;
//文件保存路徑
string strPath = strClientFilePath + @"\" + fileName;
//源文件名
string serverFileName = @"Enterprise.msi";
//傳輸數據長度,每次取字節組大小
int count = 1024 * 100;
//申請內存,存放取回的數據
byte[] buffer = new byte[count];
//文件偏移
long offset = 0;
//取回數據字節長度
int bufferSize = 1;
//-------------
while (bufferSize > 0)
{
//讀取本地文件信息
ns = new System.IO.FileStream(strPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
//獲取本地數據長度,設定從服務器取數據偏移指 針位置
offset = ns.Length;
//取服務端數據
buffer = s1.getFile(serverFileName, offset, count);
if (buffer!=null)
{
ns.Seek(offset, System.IO.SeekOrigin.Begin);
count = buffer.Length;
ns.Write(buffer, 0, count);
}
else
{
bufferSize = -1;
result = "Successful !";
}
ns.Flush();
ns.Close();
ns.Dispose();
}
return result;
}