[MessageContract]
public class DownFileResult
{
//大小
[MessageBodyMember]
public long FileSize { get; set; }
//状态
[MessageHeader]
public bool IsSuccess { get; set; }
//文件数据
[MessageBodyMember]
public byte[] Buffer { get; set; }
}
public static DownFileResult thisUploadData(string filePath, long length)
{
DownFileResult result = new DownFileResult();
long currentPosition = length;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
byte[] buffer = new byte[10240000];
fs.Position = currentPosition;
if (currentPosition + buffer.Length < fs.Length)
{
int temp = fs.Read(buffer, 0, buffer.Length);
result.Buffer = buffer;
result.FileSize = fs.Length;
result.IsSuccess = true;
fs.Flush();
fs.Close();
return result;
}
else
{
byte[] tempBuffer = new byte[fs.Length - currentPosition];
fs.Read(tempBuffer, 0, tempBuffer.Length);
result.Buffer = tempBuffer;
result.FileSize = fs.Length;
result.IsSuccess = true;
fs.Flush();
fs.Close();
return result;
}
}
}
//以上服务端代码 下面是客户端接受
/// <summary>
/// 从服务端下载数据,并且保存到客户端本地
/// </summary>
/// <param name="construction_id"></param>
public bool DownLoadData(string filePath)
{
bool rulst = false;
using (FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write))
{
try
{
long fileSize;
byte[] buffer;
string message;
do
{
//请求下载返回信息
gmapsiveClient.DownLoadGMapFile(fs.Length, "", out message, out buffer, out fileSize);
fs.Write(buffer, 0, buffer.Length);
}
while (fs.Length < fileSize);
rulst = true;
}
catch (Exception ex)
{
rulst = false;
}
finally
{
fs.Flush();
fs.Close();
}
return rulst;
}
}
#endregion