当前位置:网站首页>C # set up FTP server and realize file uploading and downloading
C # set up FTP server and realize file uploading and downloading
2022-06-25 08:01:00 【ykxxcs】
build ftp Server and realize file uploading and downloading
1、 Create a form 
2、 Introduce namespace 
windows System building FTP Server tutorial , Refer to the link below
Thanks to the author of this link :https://blog.csdn.net/weixin_43908647/article/details/113140942;
3、 The specific code is as follows :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//windows System building FTP Server tutorial , Refer to the link below
// Thanks to the author of this link :https://blog.csdn.net/weixin_43908647/article/details/113140942;
string FTPAddress = "ftp://***.***.***.***:21"; //ftp Server address
string FTPUsername = "******"; //ftp Server user name
string FTPPwd = "******";//ftp Server password
// Upload files
private void button1_Click(object sender, EventArgs e)
{
string LocalPath = "d:\\ftp\\a.txt"; // Files to be uploaded
FileInfo f = new FileInfo(LocalPath);
string FileName = f.Name;
string FTPPath = FTPAddress + "/" + FileName; //
// Implement file transfer protocol (FTP) client
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPPath));
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(FTPUsername, FTPPwd); // Set communication credentials , Input ftp Server user name and password
reqFtp.KeepAlive = false; // Close when the request is complete ftp Connect
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;// Calling the upload method
reqFtp.ContentLength = f.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// Read and upload local file data
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();
}
catch (Exception ex)
{
MessageBox.Show(" Upload failed ", ex.Message.ToString());
}
}
// Download the file
private void button2_Click(object sender, EventArgs e)
{
string FtpFilePath = "/a.txt"; // Remote path " "ftp://***.***.***.***:21"; //ftp Server address
try
{
string LocalPath = "d:\\ftp\\a.txt"; // Download to local path
MessageBox.Show(LocalPath);
if (File.Exists(LocalPath))// Determine whether the file to be downloaded exists ,
{
File.Delete(LocalPath);// If the file exists , Just delete
}
string FTPPath = FTPAddress + FtpFilePath;// original , Here is the file path of the server (FTPAddress) + The name of the file to be downloaded (FtpFilePath)
//string FTPPath = FTPAddress + "/" + LocalPath;
MessageBox.Show(FTPPath);
// establish ftp Connect
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPPath));
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(FTPUsername, FTPPwd);
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);
// Create and write files
FileStream OutputStream = new FileStream(LocalPath, FileMode.Create);
while (readCount > 0)
{
OutputStream.Write(buffer, 0, buffersize);
readCount = ftpStream.Read(buffer, 0, buffersize);
}
ftpStream.Close();
OutputStream.Close();
response.Close();
if (File.Exists(LocalPath))
MessageBox.Show(" The download ");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
边栏推荐
猜你喜欢

Do you know why the PCB produces tin beads? 2021-09-30

CAN总线工作状况和信号质量“体检”

Machine learning notes linear regression of time series

Import data into Matlab

使用报文和波形记录分析仪RoyalScope的帧统计功能排查CAN总线偶发性故障

How to use ad wiring for PCB design?

420-二叉树的层序遍历2(429. N 叉树的层序遍历、515.在每个树行中找最大值、116.填充每个节点的下一个右侧节点指针、104.二叉树的最大深度、111.二叉树的最小深度)

allgero报错:Program has encountered a problem and must exit. The design will be saved as a .SAV file

深度学习系列45:图像恢复综述
![[little knowledge] PCB proofing process](/img/bf/f66677294a14baf08cc35d1e8c1e31.jpg)
[little knowledge] PCB proofing process
随机推荐
洛谷P6822 [PA2012]Tax(最短路+边变点)
Analysis of kinsing dual platform mining family virus
现在通过开户经理发的开户链接股票开户安全吗?
Analysis and utilization of Microsoft Office Word remote command execution vulnerability (cve-2022-30190)
Import data into Matlab
VSCode很好,但我以后不会再用了
使用Adobe Acrobat Pro调整PDF页面为统一大小
电子学:第012课——实验 13:烧烤 LED
bat启动.NET Core
How to resize an image in C #
Dietary intervention reduces cancer treatment-related symptoms and toxicity
Debugging mipi-dsi screen based on stm32mp157
Ph中和过程建模
Looking for b-end product manager after years? I almost ruined myself
Matlab代码格式一键美化神器
【Unexpected token o in JSON at position 1出错原因及解决方法】
MySQL interview - the response of executing SQL is relatively slow, and the troubleshooting ideas.
Buckle 78: subset
产品经理专业知识50篇(四)-从问题到能力提升:AMDGF模型工具
消息中间件之ActiveMQ的基本使用