如何从C#windows应用程序将文件上载到SFTP服务器

如何从C#windows应用程序将文件上载到SFTP服务器

问题描述:

我有一个Windows应用程序,我需要将生成的excel文件上传到SFTP。

我的SFTP网址就像https://sftp.k.com



如果dotnet有内置功能上传到SFTP或我们需要使用任何第三方工具,请告诉我。



请以任何可能的方式指导我的步骤。



提前感谢

I have a windows Application from Which i need to upload a generated excel File To SFTP.
my SFTP url is Like https://sftp.k.com

Please let me know if dotnet have inbuilt feature to upload to SFTP or do we need To use any third party tool.

Please guide me with the steps in any of the possible way.

thanks in advance

我最近不得不使用sftp将文件上传到我的服务器。令我惊讶的是,您实际上可以使用System.Net .net库来执行此操作。 System.net包含FtpWebRequest和FtpWebResponse类,这些类证明非常有用。



上传可能如下所示:

I recently had to upload files to my server using sftp as well. To my surprise, you can actually use the System.Net .net libraries to do this. System.net contains FtpWebRequest and FtpWebResponse classes which prove to be very helpful.

Uploading might look like this:
/* Upload File */
        public void upload(string remoteFile, string localFile, string host, string user, string pass)
        {
            try
            {
                /* Create an FTP Request */
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
                /* Log in to the FTP Server with the User Name and Password Provided */
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                /* When in doubt, use these options */
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                /* Specify the Type of FTP Request */
                ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
                /* Establish Return Communication with the FTP Server */
                ftpStream = ftpRequest.GetRequestStream();
                /* Open a File Stream to Read the File for Upload */
                FileStream localFileStream = new FileStream(localFile, FileMode.Open);
                /* Buffer for the Downloaded Data */
                byte[] byteBuffer = new byte[bufferSize];
                int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
                try
                {
                    while (bytesSent != 0)
                    {
                        ftpStream.Write(byteBuffer, 0, bytesSent);
                        bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                    }
                }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
                /* Resource Cleanup */
                localFileStream.Close();
                ftpStream.Close();
                ftpRequest = null;
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            return;
        }





可以像这样使用:





and can be used like this:

upload("/public_html/testUpload.txt", @"C:\Users\1161074\Documents\testUpload.txt", @"ftp://xx.xxx.xxx.xx/", "username", "password");





我建议上一个小班,上传/下载等等,这就是我所做的。如果您愿意,请告诉我。



I suggest making a little class out of this to have upload/download, etc. Thats what I did. Let me know if you would like it.