当前位置:网站首页>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());
}
}
}
}
边栏推荐
- 力扣 272. 最接近的二叉搜索树值 II 递归
- c# winform panel自定义图片和文字
- Is it safe to open an account through the haircut account opening link now?
- 产品经理专业知识50篇(四)-从问题到能力提升:AMDGF模型工具
- Neural network and deep learning-3-simple example of machine learning pytorch
- Force deduction 76 questions, minimum covering string
- 【日常训练】207. 课程表
- Advantages and differences of three kinds of vias in PCB 2021-10-27
- Mining microbial dark matter -- a new idea
- 【红旗杯?】补题
猜你喜欢

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

Tips on how to design soft and hard composite boards ~ 22021/11/22

Mining microbial dark matter -- a new idea

TCP与UDP

Opencv daily function structure analysis and shape descriptor (8) Fitline function fitting line

Introduction to the main functions of the can & canfd comprehensive test and analysis software lkmaster of the new usbcan card can analyzer

Electronics: Lesson 011 - experiment 10: transistor switches

Technology blog | how to communicate using SSE

Looking for b-end product manager after years? I almost ruined myself

三台西门子消防主机FC18配套CAN光端机进行光纤冗余环网组网测试
随机推荐
静态网页服务器
力扣78:子集
DNS协议及其DNS完整的查询过程
【补题】2021牛客暑期多校训练营9-n
c#中设置lable控件的TextAlign属性控制文字居中的方法
传统的IO存在什么问题?为什么引入零拷贝的?
Sword finger offer II 027 Palindrome linked list
Electronics: Lesson 013 - Experiment 14: Wearable pulsed luminaries
Atlassian confluence漏洞分析合集
Invalid Navicat scheduled task
Cifar-10 dataset application: quick start data enhancement method mixup significantly improves image recognition accuracy
CAN透传云网关CANIOT,CANDTU记录CAN报文远程收发CAN数据
Matlab代码格式一键美化神器
Analysis and utilization of Microsoft Office Word remote command execution vulnerability (cve-2022-30190)
【补题】2021牛客暑期多校训练营4-n
Three Siemens fire-fighting hosts fc18 are equipped with can optical transceiver for optical fiber redundant ring network networking test
Six causes of PCB disconnection 2021-10-20
How to select lead-free and lead-free tin spraying for PCB? 2021-11-16
Analysis of kinsing dual platform mining family virus
2021ICPC网络赛第一场