简体   繁体   English

如何上传到FTP

[英]How to upload to FTP

I wanted to know how I can upload a file to an ftp but if that file already exists I wanted to override that file. 我想知道如何将文件上传到ftp,但是如果该文件已经存在,我想覆盖该文件。

This is the code I used to upload the file to ftp: 这是我用来将文件上传到ftp的代码:

var ip = myDllConfigAppSettings1.Settings["testeIP"].Value;
                            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ip + nomecompleto + ".txt");
                            request.Method = WebRequestMethods.Ftp.UploadFile;

                            request.Credentials = new NetworkCredential("test", "teste123");

                            StreamReader sourceStream = new StreamReader(arquivo.FullName);
                            byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                            sourceStream.Close();
                            request.ContentLength = fileContents.Length;

                            Stream requestStream = request.GetRequestStream();
                            requestStream.Write(fileContents, 0, fileContents.Length);
                            requestStream.Close();

                            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                            MessageBox.Show("Arquivo " + arquivo.Name + " foi enviado com sucesso. " + response.StatusDescription);

                            response.Close();

But this code does not overwrite the file that already exists. 但是此代码不会覆盖已经存在的文件。 Thanks for your help. 谢谢你的帮助。

            // connect with ftp-server
            using (WebClient client = new WebClient())
            {
                // open connection with connection-strings
                client.Credentials = new NetworkCredential(this.Username, this.Password);
                // upload zip-file on server
                client.UploadFile("ftp://localhost/"+ cleanFileName, WebRequestMethods.Ftp.UploadFile,zipFileName);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM