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 ftp
{
public partial class Form1 : Form
{
string ftpURI;
string ftpUserID;
string ftpPassword;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fg = new OpenFileDialog();
fg.RestoreDirectory = true;
if (fg.ShowDialog() == DialogResult.OK)
{
if (Upload(fg.FileName))
MessageBox.Show("上传成功");
else
MessageBox.Show("上传失败");
}
}
long t = ConvertDateTimeToInt(DateTime.Now);
public bool Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = ftpURI +t+ fileInf.Name.Substring(fileInf.Name.LastIndexOf("."));
MessageBox.Show(uri);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.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();
fs.Dispose();
return true;
}
catch (Exception ex)
{
fs.Close();
fs.Dispose();
return false;
}
}
public static long ConvertDateTimeToInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
return t;
}
private void Form1_Load(object sender, EventArgs e)
{
// 上传到ftp的位置
ftpURI = @"";
//用户名
ftpUserID = "";
//密码
ftpPassword = "";
}
}
}
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 ftp
{
public partial class Form1 : Form
{
string ftpURI;
string ftpUserID;
string ftpPassword;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fg = new OpenFileDialog();
fg.RestoreDirectory = true;
if (fg.ShowDialog() == DialogResult.OK)
{
if (Upload(fg.FileName))
MessageBox.Show("上传成功");
else
MessageBox.Show("上传失败");
}
}
long t = ConvertDateTimeToInt(DateTime.Now);
public bool Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = ftpURI +t+ fileInf.Name.Substring(fileInf.Name.LastIndexOf("."));
MessageBox.Show(uri);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.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();
fs.Dispose();
return true;
}
catch (Exception ex)
{
fs.Close();
fs.Dispose();
return false;
}
}
public static long ConvertDateTimeToInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
return t;
}
private void Form1_Load(object sender, EventArgs e)
{
// 上传到ftp的位置
ftpURI = @"";
//用户名
ftpUserID = "";
//密码
ftpPassword = "";
}
}
}