码迷,mamicode.com
首页 > 其他好文 > 详细

多文件上传简单实现

时间:2014-05-10 02:11:14      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   tar   ext   

 五一假期后的第一天上班,无聊,做了一个简单的多文件上传,如下:

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace MultiFileUploadDemo
{
    public partial class ucFileUpload : UserControl
    {
        #region 字段
        private clsFTP ftp;
        private string _filePath; 
        #endregion

        #region 构造函数
        public ucFileUpload(string _filePath)
        {
            InitializeComponent();
            this._filePath = _filePath;
            this.ftp = new clsFTP(new Uri("ftp://192.168.12.56/"), "admin", "123456");
        }
        #endregion

        #region 属性
        private WorkState _state = WorkState.Ready;
        public WorkState State
        {
            get { return _state; }
            set
            {
                _state = value;
                string msg = "";
                switch (_state)
                {
                    case WorkState.Ready:
                        msg = "准备上传";
                        break;
                    case WorkState.Running:
                        msg = "开始上传";
                        break;
                    case WorkState.Wait:
                        msg = "等待上传";
                        break;
                    case WorkState.Cancel:
                        msg = "暂停上传";
                        break;
                    case WorkState.Complete:
                        msg = "上传完成";
                        break;
                    default:
                        break;
                }
                if(!this.IsHandleCreated)
                {
                    IntPtr ptr = this.Handle;
                }
                this.Invoke(new Action(() => { lblUpload.Text = msg; }));
            }
        }

        private Action<ucFileUpload> _remove;
        public Action<ucFileUpload> Remove
        {
            get { return _remove; }
            set { _remove = value; }
        }


        #endregion

        #region 运行
        public void Run()
        {
            ftp.UploadProgressChanged += ftp_UploadProgressChanged;
            ftp.UploadFileCompleted += ftp_UploadFileCompleted;
            ftp.UploadFileAsync(_filePath, Path.GetFileName(_filePath), true);
        } 
        #endregion

        #region 上传进度变化
        
        void ftp_UploadProgressChanged(object sender, System.Net.UploadProgressChangedEventArgs e)
        {
            this.Invoke(new Action(() =>
                {
                    progressBar1.Value = e.ProgressPercentage;
                    lblUpload.Text = e.ProgressPercentage + "%";
                }));
        } 
        #endregion

        #region 上传完成
        void ftp_UploadFileCompleted(object sender, System.Net.UploadFileCompletedEventArgs e)
        {
            State = WorkState.Complete;
            if (_remove != null)
                _remove(this);
        } 
        #endregion
    }

    public enum WorkState
    { 
        Ready,
        Running,
        Wait,
        Cancel,
        Complete
    }
}
View Code
bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MultiFileUploadDemo
{
    public partial class Form1 : Form
    {
        private List<ucFileUpload> runningList = new List<ucFileUpload>();//运行列表
        private List<ucFileUpload> waitList = new List<ucFileUpload>();//等待列表
        private int maxThreadNum = 2;//最大运行线程数
        public Form1()
        {
            InitializeComponent();
        }
        //Open并选择要上传的文件
        private void btnOpen_Click(object sender, EventArgs e)
        {
            openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Multiselect = true;
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                string[] files = openFileDialog1.FileNames;
                int s = 0;
                for (int i = 0; i < files.Length; i++)
                {
                    ucFileUpload uc = new ucFileUpload(files[i]);
                    uc.Top = s;
                    s += 30;
                    if (i >= maxThreadNum)
                    {
                        uc.State = WorkState.Wait;
                        waitList.Add(uc);
                    }
                    else
                    {
                        uc.State = WorkState.Ready;
                        runningList.Add(uc);
                    }
                    uc.Remove = this.Remove;//上传完成时执行
                    panel1.Controls.Add(uc);
                }
            }
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        private void Remove(ucFileUpload uc)
        {
            runningList.Remove(uc);
            if (waitList.Count > 0)
            {
                ucFileUpload temp = waitList.First();
                temp.State = WorkState.Running;
                Thread t = new Thread(temp.Run);
                t.IsBackground = true;
                runningList.Add(temp);
                waitList.Remove(temp);
                t.Start();
            }
        }
        //上传
        private void btnUpload_Click(object sender, EventArgs e)
        {
            int index;
            for (int i = 0; i < maxThreadNum; i++)
            {
                index = i;
                Thread t = new Thread(runningList[index].Run);
                t.IsBackground = true;
                t.Start();
            }
        }
    }
}
View Code

 效果如下:

bubuko.com,布布扣

其中,ClsFTP类是某位园友封装的。

多文件上传简单实现,布布扣,bubuko.com

多文件上传简单实现

标签:style   blog   class   code   tar   ext   

原文地址:http://www.cnblogs.com/jellochen/p/3707598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!