WCF和HTTP文件传输

要求:
在同一个方案中,分别编写服务端程序和客户端程序,利用http和流传输实现文件下载功能。


1. 在IService1中设置接口,获得文件信息的方法和获得文件流对象的方法

public interface IService1
    {

        [OperationContract]
        List<string> GetFilesInfo();//字符串列表->到客户端变为数组

        [OperationContract]
        Stream DownloadStrem(string fileName);//由文件名获得文件流对象,根据文件流下载

        // TODO: 在此添加您的服务操作
    }

2. 在Service1中实现两种方法

获取文件信息

获取文件流对象并返回

public class Service1 : IService1
    {
        public static List<string> filesInfo;
        public static string path;
        public List<string> GetFilesInfo()//获取文件列表信息
        {
            if(filesInfo==null)//只在调第一次时获取
            {
                filesInfo = new List<string>();
                //获得路径信息,可执行文件目录与Foler文件夹进行拼接
                path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Folder");
                //构造DirectoryInfo对象,获得路径信息
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                //用GetFiles方法获得所有文件路径信息
                FileInfo[] q = directoryInfo.GetFiles();
                foreach (FileInfo v in q)
                {
                    filesInfo.Add(string.Format("{0},{1}",,v.Length));
                }
            }
            return filesInfo;
        }

        public Stream DownloadStrem(string fileName)//返回基类的Stream
        {
            //用文件名拼出来全文件路径信息
            string filePath = System.IO.Path.Combine(path, fileName);
            try
            {
                //构造FileStream获取文件流对象也可以
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                return fs;
                //FileStream imageFile = File.OpenRead(filePath);
                //return imageFile;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }

3. 设计界面,两个GroupBox,两个Canvas装控件,一个ListBox放文件列表,两个TextBlock,一个放正在下载的文件名,一个放下载的地址信息

http上传文件到指定nginx代理服务器_文件流


4. ShowThis方法获得文件信息并显示信息到界面

public void ShowThis()
        {
            Service1Client client = new Service1Client();
            fileInfo = client.GetFilesInfo();
            for(int i = 0; i < fileInfo.Length; i++)
            {
                string[] newstr = fileInfo[i].Split(',');
                listbox.Items.Add("文件名:"+newstr[0]+",文件大小:"+newstr[1]+"B");
            }
        }

5. 设置点击事件,初始化两个控件,对文件流读和写,并把信息传给控件显示

private void button_Click(object sender, RoutedEventArgs e)
        {
            tb1.Text = "";
            tb2.Text = "";
            string[] ss = fileInfo[listbox.SelectedIndex].Split(',');
            tb1.Text = "正在下载:"+ss[0];
            Service1Client client = new Service1Client();
            Stream stream = client.DownloadStrem(ss[0]);
            string filePath = System.IO.Path.Combine(@"E:\", ss[0]); 
            FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            byte[] bytes = new byte[1024];
            int num;
            int count0 = 0;
            do
            {
                num = stream.Read(bytes, 0, bytes.Length);
                fs.Write(bytes, 0, num);
                count0 += num;
            } while (num > 0);
            fs.Close();
            stream.Close();
            tb2.Text = "下载完成,共下载"+count0+"字节,文件保存到"+filePath;
        }

6. 配置文件App.config与Web.config增大接收与传输最大值
App.config

<bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" maxReceivedMessageSize="67108864"/>
            </basicHttpBinding>
        </bindings>

Web.config

<system.serviceModel>

    <bindings>
      <basicHttpBinding>
        <!--67108864的含义是64MB,这是HTTP每次传输允许的最大值(不指定时默认值为64KB)
            用HTTP实现时,如果希望下载大文件(例如2GB),需要先将文件按64MB切分,
            客户端分别下载后,最后再将其合并为一个文件。
            另外,也可以用TCP提供下载功能,用TCP实现时可通过配置直接下载不大于4GB的文件,
            具体实现见MSDN中的相关说明-->
        <binding name="HttpStreaming" maxReceivedMessageSize="67108864" transferMode="Streamed"/>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="MyService.Service1">
        <endpoint binding="basicHttpBinding" bindingConfiguration="HttpStreaming" contract="MyService.IService1"/>
      </service>
    </services>

问题讨论:

缓冲区bytes大小不同,导致下载的图片效果不同且出现错误,改到6kb问题消失

原代码:

http上传文件到指定nginx代理服务器_文件名_02


原因:在最后一次读取到bytes时,剩余的文件信息不足bytes,然后导致上一次读的文件的部分信息没有得到更新,实际读出的长度是num,所以把write的bytes.Length改为num。

http上传文件到指定nginx代理服务器_c#_03