LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

winform下用FileStream实现多文件上传

admin
2017年3月22日 0:41 本文热度 6018

先看效果图:


代码:

[csharp] view plain copy
 print?
  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,自己去网上下。


该文章在 2017/3/22 0:41:28 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved