服务器端:
[WebMethod]
public byte[] DownloadFile(string file)
{
System.IO.FileStream fs = null;
if (System.IO.File.Exists(file))
{
try
{
fs = System.IO.File.OpenRead(file);
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1 = fs.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
}
catch (Exception ex)
{
return new byte[0];
}
finally
{
fs.Close();
}
}
else
{
return new byte[0];
}
}
客户端:
/// <summary>
/// 从服务器上下载文件,未用
/// </summary>
/// <param name="str1">源文件:如“E:\\city.txt”</param>
/// <param name="str2">目标文件:如@"d:\city.txt"</param>
public static void DownFile(string str1, string str2)
{
CRMweb.WebService1SoapClient myWeb = new CRMweb.WebService1SoapClient();
byte[] bs = myWeb.DownloadFile(str1);
System.IO.FileStream stream = new System.IO.FileStream(str2, System.IO.FileMode.CreateNew);
stream.Write(bs, 0, bs.Length);
stream.Flush();
stream.Close();
}
本文详细介绍了如何在服务器端实现文件的下载功能,并在客户端进行相应的文件接收和保存操作。通过具体的代码示例,展示了使用.NET框架下,如何将文件转换为字节数组在网络中传输,以及在客户端如何将接收到的字节数组还原为文件。
514

被折叠的 条评论
为什么被折叠?



