winform中通过FileStream实现将文件上传
本实例实现功能:通过OpenFileDialog选择待上传的文件,并将所选文件的完整路径绑定到TreeView控间中显示,然后通过FolderBrowserDialog选择上传的文件路径,最后通过FileStream的方法将文件以二进制流的形式写入到所选路径的对应文件中。
其中:
trvFile为TreeView控间,显示待上传的文件;lablContent为Lable控间,显示待上传文件的信息和上传结果;btnSearch为Button控间,执行选择文件的功能;btnUpload为Button控间,执行文件上传的功能。
详细代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication1
{
public partial class FileUpLoad : Form
{
public FileUpLoad()
{
InitializeComponent();
this.lablContent.Text = "附件名称:";
}
private void btnSearch_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
//得到上传文件的完整名
string loadFullName = ofd.FileName.ToString();
//上传文件的文件名
string loadName = loadFullName.Substring(loadFullName.LastIndexOf("//") + 1);
//上传文件的类型
string loadType = loadFullName.Substring(loadFullName.LastIndexOf(".") + 1);
this.lablContent.Text += "/n"+loadFullName;
AddFileToTreeView(loadFullName, loadName);
}
选择文件完成后的,用户界面如下图所示:
/// <summary>
/// 上传文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void btnUpload_Click(object sender, EventArgs e)
{
TreeNodeCollection tnNodeColl = trvFile.Nodes;
int nodeCount = tnNodeColl.Count;
int successCount = 0;
if (nodeCount > 0)
{
//
//选择保存文件的路径
//
folderBrowserDialog1.ShowDialog();
string loadPath = folderBrowserDialog1.SelectedPath;
string nodeText = string.Empty;
string nodeTipText = string.Empty;
foreach (TreeNode node in tnNodeColl)
{
if (node.Checked)
{
nodeText = node.Text.ToString();
nodeTipText = node.ToolTipText;
string loadFile = loadPath + "//"+nodeTipText;
bool isExists = JudgeFileExists(loadFile, nodeTipText);
if (isExists)
{
byte[] btFile = FileToBinary(nodeText);
if (BinaryToFile(loadFile, btFile))
{
node.Checked = false;
successCount++;
continue;
}
else
{
break;
}
}
else
{
continue;
}
}
else
{
continue;
}
}
string strCue = "成功上传" + successCount.ToString() + "个文件。/n";
int failCount = nodeCount - successCount;
strCue += "上传失败" + failCount.ToString() + "个文件。/n";
this.lablContent.Text = strCue;
}
else
{
MessageBox.Show("请选择要上传的文件!");
}
}
上传地址选择界面,如下图所示:
上传成功界面,如下图所示:
/// <summary>
/// 将文件转换为二进制流进行读取
/// </summary>
/// <param name="fileName">文件完整名</param>
/// <returns>二进制流</returns>
private byte[] FileToBinary(string fileName)
{
try
{
FileStream fsRead = new FileStream(fileName, FileMode.Open, FileAccess.Read);
if (fsRead.CanRead)
{
int fsSize = Convert.ToInt32(fsRead.Length);
byte[] btRead = new byte[fsSize];
fsRead.Read(btRead, 0, fsSize);
return btRead;
}
else
{
MessageBox.Show("文件读取错误!");
return null;
}
}
catch (Exception ce)
{
MessageBox.Show(ce.Message);
return null;
}
}
/// <summary>
/// 将二进制流转换为对应的文件进行存储
/// </summary>
/// <param name="filePath">接收的文件</param>
/// <param name="btBinary">二进制流</param>
/// <returns>转换结果</returns>
private bool BinaryToFile(string fileName, byte[] btBinary)
{
bool result = false;
try
{
FileStream fsWrite = new FileStream(fileName, FileMode.Create, FileAccess.Write);
if (fsWrite.CanWrite)
{
fsWrite.Write(btBinary, 0, btBinary.Length);
MessageBox.Show("文件写入成功!");
result = true;
}
else
{
MessageBox.Show("文件些入错误!");
result = false;
}
}
catch (Exception ce)
{
MessageBox.Show(ce.Message);
result = false;
}
return result;
}
/// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="fileName">文件完整的路径名</param>
/// <param name="nodeTipText">文件名</param>
/// <returns>判断结果</returns>
private bool JudgeFileExists(string fileName, string nodeTipText)
{
if (File.Exists(fileName))
{
StringBuilder sbError = new StringBuilder();
sbError.Append(nodeTipText + "已存在于:/n");
sbError.Append(fileName.Substring(0, fileName.LastIndexOf("//")) + "/n");
sbError.Append("中,是否覆盖原文件?");
string strSearch = MessageBox.Show(sbError.ToString(), "提示", MessageBoxButtons.YesNo).ToString();
if (strSearch == "Yes")
{
return true;
}
else
{
return false;
}
}
else
{
return true;
}
}
/// <summary>
/// 为树菜单赋值,即待上传文件的完整名
/// </summary>
/// <param name="fullName"></param>
/// <param name="simpleName"></param>
private void AddFileToTreeView(string fullName, string simpleName)
{
TreeNodeCollection nodeCollection = trvFile.Nodes;
TreeNode node = new TreeNode();
node.Text = fullName;
node.ToolTipText = simpleName;
node.Checked = true;
nodeCollection.Add(node);
if (nodeCollection.Count == 1)
{
Button btnUpload = new Button();
btnUpload.Text = "上传";
btnUpload.Click += new EventHandler(btnUpload_Click);
panel1.Controls.Add(btnUpload);
}
}
}
}