分享

winform下用FileStream实现多文件上传

 feisguan 2018-05-25

先看效果图:

代码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.IO;  
  10. using System.Collections;  
  11. using System.Threading;  
  12.   
  13. namespace VideoTrans  
  14. {  
  15.     public partial class Form2 : Form  
  16.     {  
  17.         public Form2()  
  18.         {  
  19.             InitializeComponent();  
  20.             LoadListView();  
  21.             this.FormClosed += (sender, e) => {  
  22.                 Application.Exit();  
  23.                 System.Diagnostics.Process pro = System.Diagnostics.Process.GetCurrentProcess();  
  24.                 pro.Kill();  
  25.             };  
  26.         }  
  27.         /// <summary>  
  28.         /// 初始化上传列表  
  29.         /// </summary>  
  30.         void LoadListView()  
  31.         {  
  32.             listView1.View = View.Details;  
  33.             listView1.CheckBoxes = true;  
  34.             listView1.GridLines = true;  
  35.             listView1.Columns.Add("文件名",150,HorizontalAlignment.Center);  
  36.             listView1.Columns.Add("文件大小", 150, HorizontalAlignment.Center);  
  37.             listView1.Columns.Add("文件路径", 150, HorizontalAlignment.Center);  
  38.         }  
  39.         /// <summary>  
  40.         /// 存储目录  
  41.         /// </summary>  
  42.         /// <param name="sender"></param>  
  43.         /// <param name="e"></param>  
  44.         private void button2_Click(object sender, EventArgs e)  
  45.         {  
  46.             FolderBrowserDialog fbd = new FolderBrowserDialog();  
  47.             if(fbd.ShowDialog()==DialogResult.OK)  
  48.             {  
  49.                 textBox1.Text=fbd.SelectedPath;  
  50.             }  
  51.         }  
  52.         /// <summary>  
  53.         /// 打开上传文件  
  54.         /// </summary>  
  55.         /// <param name="sender"></param>  
  56.         /// <param name="e"></param>  
  57.         private void button3_Click(object sender, EventArgs e)  
  58.         {  
  59.               
  60.             OpenFileDialog ofd = new OpenFileDialog();  
  61.             ofd.Multiselect = true;  
  62.             if(ofd.ShowDialog()==DialogResult.OK)  
  63.             {  
  64.                 foreach(string filename in ofd.FileNames)  
  65.                 {  
  66.                     FileInfo fi=new FileInfo(filename);  
  67.                     ListViewItem lvi = new ListViewItem(Path.GetFileNameWithoutExtension(filename));  
  68.                     lvi.Tag = filename;  
  69.                     lvi.SubItems.Add(fi.Length.ToString());  
  70.                     //lvi.SubItems.Add((fi.Length / (1024 * 1024)).ToString() + "M");  
  71.                     lvi.SubItems.Add(Path.GetDirectoryName(filename));  
  72.                     listView1.Items.Add(lvi);  
  73.                 }  
  74.                   
  75.             }  
  76.         }  
  77.         public delegate void DeleFile(int position);  
  78.         /// <summary>  
  79.         /// 文件上传  
  80.         /// </summary>  
  81.         /// <param name="sender"></param>  
  82.         /// <param name="e"></param>  
  83.         private void button1_Click(object sender, EventArgs e)  
  84.         {  
  85.   
  86.             if (textBox1.Text.Trim().Equals(""))  
  87.             {  
  88.                 MessageBox.Show("请先选择存储目录..!");  
  89.             }  
  90.             else  
  91.             {  
  92.                   
  93.                 if (listView1.Items.Count > 0)  
  94.                 {  
  95.                     int j = 0;  
  96.                     string count = listView1.CheckedItems.Count.ToString();  
  97.                     for (int i = 0; i < listView1.Items.Count;i++ )  
  98.                     {  
  99.                           
  100.                         if (listView1.Items[i].Checked)  
  101.                         {  
  102.                             j++;  
  103.                             string fileName = Path.GetFileName(listView1.Items[i].Tag.ToString());  
  104.                             label1.Text = string.Format("正在上传文件:[{0}]", listView1.Items[i].Text) + ":" + j.ToString() + ":" + count;  
  105.                             FileStream des = new FileStream(Path.Combine(textBox1.Text, fileName), FileMode.OpenOrCreate, FileAccess.Write);  
  106.                             FileStream fir = new FileStream(listView1.Items[i].Tag.ToString(), FileMode.Open, FileAccess.Read);  
  107.                             byte[] buffer = new byte[10240];  
  108.                             int size = 0; int ren = 0;  
  109.                             while (ren < fir.Length)  
  110.                             {  
  111.                                 Application.DoEvents();  
  112.                                 size = fir.Read(buffer,0,buffer.Length);  
  113.                                 des.Write(buffer, 0, size);  
  114.                                 ren += size;  
  115.                                 Pro(ren);  
  116.                             }                 
  117.                             listView1.Items[i].Checked = false;  
  118.                         }  
  119.                         else  
  120.                         {  
  121.                             continue;  
  122.                         }       
  123.                     }  
  124.                     //MessageBox.Show("上传成功!");  
  125.                 }  
  126.                 else  
  127.                 {  
  128.                     return;  
  129.                 }  
  130.             }       
  131.              
  132.         }  
  133.         public void Pro(int copy)  
  134.         {  
  135.         
  136.             if (this.progressBarEx1.InvokeRequired)  
  137.             {  
  138.                 this.progressBarEx1.Invoke(new DeleFile(Pro),new object[]{copy});  
  139.                 return;  
  140.             }  
  141.             foreach (ListViewItem lvi in listView1.CheckedItems)  
  142.             {  
  143.                 string total = lvi.SubItems[1].Text;  
  144.                 int pro = (int)((float)copy / long.Parse(total) * 100);  
  145.                 if (pro <= progressBarEx1.Maximum)  
  146.                 {  
  147.                     progressBarEx1.Value = pro;  
  148.                     progressBarEx1.Text = label1.Text.Split(':')[0].ToString() + Environment.NewLine + string.Format("上传进度:{0}%", pro) + Environment.NewLine + string.Format("已上传文件数:{0}/{1}", label1.Text.Split(':')[1].ToString(), label1.Text.Split(':')[2].ToString());  
  149.                       
  150.                 }  
  151.             }  
  152.               
  153.         }  
  154.         /// <summary>  
  155.         /// 删除文件  
  156.         /// </summary>  
  157.         /// <param name="sender"></param>  
  158.         /// <param name="e"></param>  
  159.         private void button4_Click(object sender, EventArgs e)  
  160.         {  
  161.             foreach(ListViewItem lvi in listView1.CheckedItems)  
  162.             {  
  163.                 lvi.Remove();  
  164.             }  
  165.         }  
  166.         
  167.     }  
  168. }  



新手们好好理解,整个程序没什么技术难度,里面的进度条为第三方控件ProgressODoom.dll,自己去网上下。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多