在窗体中如何实现文件的上传下载?上传下载的相应文件都存储在某个磁盘里。
找了一些代码,可是无法实现,大家帮帮啊!急用!
找了一些代码,可是无法实现,大家帮帮啊!急用!
解决方案 »
- SQL小疑问!
- c# udpclient 的recv时间问题
- 刷新后TextBox的值消失了 怎么解决???
- winCE系统下,cs客户端webBrowser如何打开doc文件?
- SOCKET接收数据超时
- 正则表达式(求救,立即给分)
- 请教,如何用代码实现切换选现卡。
- 又一treeView的问题,如何判断父节点下有多少子节点!谢谢急。。。。。。。。。。。。
- 这里有入门教学区或讨论社区吗?
- 线程计时器是使用回调方法而不是事件,回调方法和事件有什么区别? SAX 如何实现?.net 中有此命名空间?和XmlReader区别?
- 怎么在DataGridView最后一行加上统计
- 我想知道哪个服务器的压力最大
下载方法你可以选WebClient,上传你可以试试FTP上传下载
要在服务器端设置Ftp文件夹,要的我有源码
代码在msdn,还是自动脑好,看看msdn的System.IO和System.Net命名空间下相关的类也都有示例
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.Windows.Forms.Design;
using System.IO;namespace CreateDirectory
{
public partial class OpenSave : Form
{
public string srcdir;
public string desdir; public OpenSave()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
} private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "图片文件|*.bmp;*.jpg;*.jpeg;*.gif;*.png|*.doc|*.doc";
openFileDialog1.Title = "Select a file";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.Diagnostics.Process.Start(openFileDialog1.FileName);
} } private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog(); if (saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile method.
System.IO.FileStream fs =(System.IO.FileStream)saveFileDialog1.OpenFile();
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch (saveFileDialog1.FilterIndex)
{
case 1:
{
//Form1 form1 = new Form1();
//form1.button1.Image.Save(fs,System.Drawing.Imaging.ImageFormat.Jpeg);
//form1.button1.Image.Save(fs, System.Drawing.ImageFormat.Jpeg);
//this.button2.Image.Save(fs,
// System.Drawing.Imaging.ImageFormat.Jpeg);
}
break; case 2:
this.button2.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break; case 3:
this.button2.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Gif);
break;
} fs.Close();
} } private void button3_Click(object sender, EventArgs e)
{
CopyDirectory(srcdir, desdir);
} private void button4_Click(object sender, EventArgs e)
{
FolderDialog aa = new FolderDialog();
aa.DisplayDialog();
srcdir = aa.Path;
//MessageBox.Show(aa.Path); //OpenFileDialog srcfile = new OpenFileDialog();
//srcfile.ShowDialog();
//srcdir = srcfile.FileName;
} private void button5_Click(object sender, EventArgs e)
{
FolderDialog bb = new FolderDialog();
bb.DisplayDialog();
desdir = bb.Path;
} private void CopyDirectory(string srcdir, string desdir)
{
string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1); string desfolderdir = desdir + "\\" + folderName; if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
{
desfolderdir = desdir + folderName;
}
string[] filenames = Directory.GetFileSystemEntries(srcdir); foreach (string file in filenames)// 遍历所有的文件和目录
{
if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
{ string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
if (!Directory.Exists(currentdir))
{
Directory.CreateDirectory(currentdir);
} CopyDirectory(file, desfolderdir);
} else // 否则直接copy文件
{
string srcfileName = file.Substring(file.LastIndexOf("\\") + 1); srcfileName = desfolderdir + "\\" + srcfileName;
if (!Directory.Exists(desfolderdir))
{
Directory.CreateDirectory(desfolderdir);
}
File.Copy(file, srcfileName);
}
}
}
}
class FolderDialog : FolderNameEditor
{
FolderBrowser fDialog = new FolderBrowser();
public FolderDialog()
{
}
public DialogResult DisplayDialog()
{
return DisplayDialog("确定你的文件夹路径!!");
}
public DialogResult DisplayDialog(string description)
{
fDialog.Description = description;
return fDialog.ShowDialog();
}
public string Path
{
get
{
return fDialog.DirectoryPath;
}
}
~FolderDialog()
{
fDialog.Dispose();
}
}}
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;namespace FileUpLoad
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); this.lblContext.Text = "附件名称:";
} private void btnSearch_Click(object sender, EventArgs e)
{
OpenFileDialog openfd = new OpenFileDialog(); openfd.ShowDialog(); string uploadName = openfd.FileName; string loadName = uploadName.Substring(uploadName.LastIndexOf("\\") + 1); string loadType = uploadName.Substring(uploadName.LastIndexOf(".") + 1); this.lblContext.Text += "\n" + uploadName; AddFileToTreeView(uploadName, loadName); } private void AddFileToTreeView(string fullName, string simpleName)
{
TreeNodeCollection nodeCollection = treeFile.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); }
} void btnUpload_Click(object sender, EventArgs e)
{
TreeNodeCollection tnNodeColl = treeFile.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.lblContext.Text = strCue;
}
else
{
MessageBox.Show("请选择要上传的文件!");
}
} 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;
} } 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;
}
} 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;
} }
}
namespace FileUpLoad
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnSearch = new System.Windows.Forms.Button();
this.lblContext = new System.Windows.Forms.Label();
this.treeFile = new System.Windows.Forms.TreeView();
this.panel1 = new System.Windows.Forms.Panel();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.SuspendLayout();
//
// btnSearch
//
this.btnSearch.Location = new System.Drawing.Point(374, 208);
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(108, 23);
this.btnSearch.TabIndex = 0;
this.btnSearch.Text = "选择上传文件";
this.btnSearch.UseVisualStyleBackColor = true;
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// lblContext
//
this.lblContext.AutoSize = true;
this.lblContext.ForeColor = System.Drawing.Color.Red;
this.lblContext.Location = new System.Drawing.Point(12, 251);
this.lblContext.Name = "lblContext";
this.lblContext.Size = new System.Drawing.Size(0, 12);
this.lblContext.TabIndex = 3;
//
// treeFile
//
this.treeFile.Location = new System.Drawing.Point(12, 12);
this.treeFile.Name = "treeFile";
this.treeFile.Size = new System.Drawing.Size(575, 188);
this.treeFile.TabIndex = 4;
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(503, 208);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(84, 21);
this.panel1.TabIndex = 5;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(599, 445);
this.Controls.Add(this.lblContext);
this.Controls.Add(this.panel1);
this.Controls.Add(this.treeFile);
this.Controls.Add(this.btnSearch);
this.Name = "Form1";
this.Text = "FileUpload";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button btnSearch;
private System.Windows.Forms.Label lblContext;
private System.Windows.Forms.TreeView treeFile;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
}
}