【.NET】转载:使用FileStream批量上传文件。
先看效果图:
代码(没什么难度的一个案例,进度条为第三方控件ProgressODoom.dll,自己去网上下。)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Threading; namespace VideoTrans
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
LoadListView();
this.FormClosed += (sender, e) => {
Application.Exit();
System.Diagnostics.Process pro = System.Diagnostics.Process.GetCurrentProcess();
pro.Kill();
};
}
/// <summary>
/// 初始化上传列表
/// </summary>
void LoadListView()
{
listView1.View = View.Details;
listView1.CheckBoxes = true;
listView1.GridLines = true;
listView1.Columns.Add("文件名",,HorizontalAlignment.Center);
listView1.Columns.Add("文件大小", , HorizontalAlignment.Center);
listView1.Columns.Add("文件路径", , HorizontalAlignment.Center);
}
/// <summary>
/// 存储目录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if(fbd.ShowDialog()==DialogResult.OK)
{
textBox1.Text=fbd.SelectedPath;
}
}
/// <summary>
/// 打开上传文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{ OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if(ofd.ShowDialog()==DialogResult.OK)
{
foreach(string filename in ofd.FileNames)
{
FileInfo fi=new FileInfo(filename);
ListViewItem lvi = new ListViewItem(Path.GetFileNameWithoutExtension(filename));
lvi.Tag = filename;
lvi.SubItems.Add(fi.Length.ToString());
//lvi.SubItems.Add((fi.Length / (1024 * 1024)).ToString() + "M");
lvi.SubItems.Add(Path.GetDirectoryName(filename));
listView1.Items.Add(lvi);
} }
}
public delegate void DeleFile(int position);
/// <summary>
/// 文件上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{ if (textBox1.Text.Trim().Equals(""))
{
MessageBox.Show("请先选择存储目录..!");
}
else
{ if (listView1.Items.Count > )
{
int j = ;
string count = listView1.CheckedItems.Count.ToString();
for (int i = ; i < listView1.Items.Count;i++ )
{ if (listView1.Items[i].Checked)
{
j++;
string fileName = Path.GetFileName(listView1.Items[i].Tag.ToString());
label1.Text = string.Format("正在上传文件:[{0}]", listView1.Items[i].Text) + ":" + j.ToString() + ":" + count;
FileStream des = new FileStream(Path.Combine(textBox1.Text, fileName), FileMode.OpenOrCreate, FileAccess.Write);
FileStream fir = new FileStream(listView1.Items[i].Tag.ToString(), FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[];
int size = ; int ren = ;
while (ren < fir.Length)
{
Application.DoEvents();
size = fir.Read(buffer,,buffer.Length);
des.Write(buffer, , size);
ren += size;
Pro(ren);
}
listView1.Items[i].Checked = false;
}
else
{
continue;
}
}
//MessageBox.Show("上传成功!");
}
else
{
return;
}
} }
public void Pro(int copy)
{ if (this.progressBarEx1.InvokeRequired)
{
this.progressBarEx1.Invoke(new DeleFile(Pro),new object[]{copy});
return;
}
foreach (ListViewItem lvi in listView1.CheckedItems)
{
string total = lvi.SubItems[].Text;
int pro = (int)((float)copy / long.Parse(total) * );
if (pro <= progressBarEx1.Maximum)
{
progressBarEx1.Value = pro;
progressBarEx1.Text = label1.Text.Split(':')[].ToString() + Environment.NewLine + string.Format("上传进度:{0}%", pro) + Environment.NewLine + string.Format("已上传文件数:{0}/{1}", label1.Text.Split(':')[].ToString(), label1.Text.Split(':')[].ToString()); }
} }
/// <summary>
/// 删除文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
foreach(ListViewItem lvi in listView1.CheckedItems)
{
lvi.Remove();
}
} }
}
转自:http://blog.csdn.net/wanlong360599336/article/details/7564508
【.NET】转载:使用FileStream批量上传文件。的更多相关文章
- Linux命令之rz - 批量上传文件,简单易用(转载)
用途说明 rz命令能够批量上传文件,当然也可上传单个文件啦.使用的协议是古老的ZMODEM协议,尽管协议古老,但毫不影响的简单易用的特性.一般情 况我们要上传文件到Linux系统,要么使用ftp(还得 ...
- 转 Android网络编程之使用HttpClient批量上传文件 MultipartEntityBuilder
请尊重他人的劳动成果,转载请注明出处:Android网络编程之使用HttpClient批量上传文件 http://www.tuicool.com/articles/Y7reYb 我曾在<Andr ...
- 使用 sendKeys(keysToSend) 批量上传文件
未经允许,禁止转载!!! 在selenium里面处理文件上传的时候可以使用sendKeys(keysToSend) 上传文件 例如: element.sendKeys(“C:\\test\\uploa ...
- 不带插件 ,自己写js,实现批量上传文件及进度显示
今天接受项目中要完成文件批量上传文件而且还要显示上传进度,一开始觉得这个应该不是很麻烦,当我在做的时候遇到了很多问题,很头疼啊. 不过看了别人写的代码,自己也测试过,发现网上好多都存在一些问题,并不是 ...
- Python基于Python实现批量上传文件或目录到不同的Linux服务器
基于Python实现批量上传文件或目录到不同的Linux服务器 by:授客 QQ:1033553122 实现功能 1 测试环境 1 使用方法 1 1. 编辑配置文件conf/rootpath_fo ...
- input file multiple 批量上传文件
这几天维护系统,有一个批量上传文件功能,出现了一点小问题 我的笔记本选择要上传的文件很正常 但在测试环境上,别人的电脑上,选择上传文件之后 一开始,以为是代码问题,网上找了很多的资料,但还是没用,然后 ...
- TP3.2批量上传文件(图片),解决同名冲突问题
1.html <form action="{:U('Upload/index')}" enctype="multipart/form-data" meth ...
- 用Azure CLI批量上传文件
在Windows环境下,我们可以使用AzCopy批量上传文件.其效率和传输速率都是非常快的. 在Linux或MacOS环境下,可以使用Azure的CLI实现批量文件的上传. 下面的脚本可以实现此功能. ...
- Java Miniui实现批量上传文件demo 201906221520
可能需要的jar包: 需要miniui(类似easyui). Test2019062201.jsp <%@ page language="java" contentType= ...
随机推荐
- gcc与g++的区别与联系
gcc和g++都是GNU(组织)的编译器. 一.误区详解 误区一:gcc只能编译c代码,g++只能编译c++代码 两者都可以,但是请注意: 1.后缀为.c的文件, gcc把它当作是C程序,而g++ ...
- magnum devstack部署
magnum安装 安装条件: 至少要10G以上内存的机器.亲测使用6G的虚拟机,所有操作均有至少一秒延迟. 硬盘至少50G 良好的上网环境 操作步骤参见快速入门 以下是我操作的步骤记录 sudo mk ...
- DNS解析详细过程
英文:domain name system. 中文:域名系统. 解析过程: 第一次请求站点(例:http://www.baidu.com),先在当前浏览器路径下寻找有没有缓存对应的解析结果,如果有的话 ...
- 在egret中使用protobuf
原文章删除,重新使用MarkDown排版 在H5游戏领域,对于服务端与客户端的通信协议有一个选择,那就是使用protobuf.js.对于那些直接使用JavaScript开发的引擎而言,protobuf ...
- C语言之进制
进制 一种计数的方式.侧重点在于计数的时候是逢多少进一. 1) C语言可以识别的进制 二进制 每一位. 0 0 1 0 0 1 1 0 1 0 1 0 0 1 0 1 1 0 在C语言中,写 ...
- [Angular Directive] Implement Structural Directive Data Binding with Context in Angular
Just like in *ngFor, you're able to pass in data into your own structural directives. This is done b ...
- Python wifi掉线重连接
原理很简单,通过python执行dos命令 : ping 和 netsh 需要用到os和time模块 代码如下: >>> import os >>> print ' ...
- java 的序列化
(1) 首先是java自己内部实现的对象序列化机制 其实就是ObjectInputStream 和 ObjectOutputStream 首先实现一个实体对象 记住必须实现Serializable ...
- 一个App从创意到最终上架到App Store里的整个过程是怎样的?
一个App从创意到最终上架到App Store里的整个过程是怎样的? 制作App需要什么软件?应该看什么书?需要哪些设备?推到App Store里,需要注册什么网站?是否需要付费?需要什么证书之类的? ...
- 【python标准库】内建函数
abs(x) 返回一个数的绝对值.参数可以是普通的整数,长整数或者浮点数.如果参数是个复数,返回它的模. all(iterable) 如果iterable的所有元素为真(或者iterable为空), ...