基本信息
源码名称:WCF和HTTP文件传输练习(支持下载大文件)
源码大小:0.43M
文件格式:.zip
开发语言:C#
更新时间:2019-06-23
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Client;
using Client.fileService;
namespace Client
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        string savePath = "D:/test/";
        FileInfomation[] l;
        public MainWindow()
        {
            InitializeComponent();
            
        }

        private void viewMenu_Click(object sender, RoutedEventArgs e)
        {
            fileListBox.Items.Clear();
            Service1Client myFileService = new Service1Client();
            l = myFileService.GetFileList();
            foreach (var item in l)
            {
                fileListBox.Items.Add("文件名:" item.FileName ",文件长度:" item.FileSize "字节");
            }
        }

        private void startDownload_Click(object sender, RoutedEventArgs e)
        {
            bool isexit = false;
            int index = fileListBox.SelectedIndex;
            //fileList fl = (fileList)fileListBox.SelectedItems;
            
            Service1Client myFileService = new Service1Client();
            Stream sourceStream = myFileService.Download(l[index].FileName);

            if(sourceStream !=null)
            {
                if (sourceStream.CanRead)
                {
                    string saveFullPath = savePath   l[index].FileName;
                    FileStream fs = new FileStream(saveFullPath, FileMode.Create, FileAccess.Write, FileShare.None);
                    const int bufferLength = 4096;
                    byte[] myBuffer = new byte[bufferLength];

                    lb_filename.Content = "正在下载 "   l[index].FileName;
                    int count;
                    while ((count = sourceStream.Read(myBuffer, 0, bufferLength)) > 0)
                    {
                        if (isexit== false)
                        {
                            fs.Write(myBuffer, 0, count);
                        }
                        else//窗体已经关闭跳出循环
                        {
                            break;
                        }
                    }
                    fs.Close();
                    sourceStream.Close();
                    lb_filepath.Content = "下载完成,共下载"   l[index].FileSize   "字节,文件保存到 "   saveFullPath;
                }
            }
        }
    }
}