using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FtpWin
{
public partial class Form1 : Form
{
string FtpUrl = "ftp://192.168.0.104:2018/";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = true;
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
string[] fileNameList = openFileDialog1.FileNames;
string filename = "";
foreach (var item in fileNameList)
{
if (!string.IsNullOrEmpty(filename))
{
filename += ";" +Path.GetFileName(item.ToString());
}
else
{
filename =Path.GetFileName(item.ToString());
}
}
textBox1.Text = filename;
string FilePath = this.openFileDialog1.FileName;
UploadFile(FilePath);
}
}
private void button2_Click(object sender, EventArgs e)
{
saveFileDialog1.FileName = @"D:\123.txt";
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string FilePath= saveFileDialog1.FileName;
Download(FilePath);
}
}
#region 本地文件上传到FTP服务器
/// <summary>
/// 上传文件到远程ftp
/// </summary>
/// <param name="path">本地的文件目录</param>
/// <param name="name">文件名称</param>
/// <returns></returns>
public void UploadFile(string path)
{
string name = Path.GetFileName(path);
FileInfo f = new FileInfo(path);
path = path.Replace("\\", "/");
string FtpFilePath = FtpUrl+"/"+DateTime.Now.ToString("yyyyMMdd");
//先创建文件夹
FtpWebRequest reqFtp1 = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpFilePath));
reqFtp1.UseBinary = true;
reqFtp1.Method = WebRequestMethods.Ftp.MakeDirectory;
//创建完的文件夹与FTP路径结合形成文件的路径
path = FtpFilePath + "//" + name;
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
reqFtp.UseBinary = true;
reqFtp.KeepAlive = false;
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
reqFtp.ContentLength = f.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = f.OpenRead();
try
{
Stream strm = reqFtp.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
MessageBox.Show("上传完成!");
}
catch (Exception ex)
{
MessageBox.Show("因{0},无法完成上传", ex.Message);
}
}
#endregion
#region 从ftp服务器下载文件
/// <summary>
/// 从ftp服务器下载文件的功能
/// 在与数据库结合的时候获取数据 读取数据的路径+文件名
///
/// </summary>
/// <param name="filePath">数据库文件的地址</param>
/// <returns></returns>
public void Download(string filePath)
{
try
{
String onlyFileName = Path.GetFileName(filePath);
//下载在本地路径地址 newFileName
string newFileName =Path.GetDirectoryName( filePath )+ onlyFileName;
if (File.Exists(newFileName))
{
File.Delete(newFileName);
}
FtpUrl = FtpUrl.Replace("\\", "/");
//暂时写死的 用到数据库的时候写成数据库保存的路径+文件的名称 FTP也可以是可以配置的比较方便
string url = FtpUrl + "/123/111/" + onlyFileName;
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFtp.UseBinary = true;
//ftp登录用户名和
//reqFtp.Credentials = new NetworkCredential(FTPUSERNAME, FTPPASSWORD);
FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
FileStream outputStream = new FileStream(newFileName, FileMode.Create);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
}
}
#endregion
}
}