Asked by:
How to code to upload to FTP server?

Question
-
User546194788 posted
For web project how to code to upload to FTP server?
Does need third party library?Thursday, February 6, 2020 8:51 PM
All replies
-
User753101303 posted
Hi,
You could try first https://docs.microsoft.com/en-us/dotnet/api/system.net.ftpwebrequest?view=netframework-4.8
Edit: scrolling down I see they recommend using 3rd party librairies for new development with a link that ultimately points to https://stackoverflow.com/questions/1371964/free-ftp-library
Thursday, February 6, 2020 8:59 PM -
User665608656 posted
Hi aspfun,
If you want to upload files to the FTP server through a web project, you don't need to download a third-party library.
You can use the following code to achieve your needs:
<asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button Text="Upload to FTP" runat="server" ID="upload" OnClick="upload_Click" /> <hr /> <asp:Label ID="lblMessage" runat="server" />
using System; using System.IO; using System.Net; using System.Text; namespace WebApplication_webform { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void upload_Click(object sender, EventArgs e) { //FTP Server URL. string ftp = "ftp://yourserver.com/"; //FTP Folder name. Leave blank if you want to upload to root folder. string ftpFolder = "Uploads/"; byte[] fileBytes = null; //Read the FileName and convert it to Byte array. string fileName = Path.GetFileName(FileUpload1.FileName); using (StreamReader fileStream = new StreamReader(FileUpload1.PostedFile.InputStream)) { fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd()); fileStream.Close(); } try { //Create FTP Request. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName); request.Method = WebRequestMethods.Ftp.UploadFile; //Enter FTP Server credentials. request.Credentials = new NetworkCredential("UserName", "Password"); request.ContentLength = fileBytes.Length; request.UsePassive = true; request.UseBinary = true; request.ServicePoint.ConnectionLimit = fileBytes.Length; request.EnableSsl = false; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(fileBytes, 0, fileBytes.Length); requestStream.Close(); } FtpWebResponse response = (FtpWebResponse)request.GetResponse(); lblMessage.Text += fileName + " uploaded.<br />"; response.Close(); } catch (WebException ex) { throw new Exception((ex.Response as FtpWebResponse).StatusDescription); } } } }
Best Regards,
YongQing.
Friday, February 7, 2020 5:59 AM -
User546194788 posted
The project I created is using SSH.NET and it works fine for two years.
But, after Windows update it did not work anymore.
The error message is: Renci.SshNet.Common.SshOperationTimeoutException: Session operation has timed out
SSH.NET version I used is 2016.0.0 for workframe 4.0 only.
I keep on searching but no luck.
Friday, February 7, 2020 1:51 PM -
User665608656 posted
Hi aspfun,
The error message is: Renci.SshNet.Common.SshOperationTimeoutException: Session operation has timed outFor this issue, you can refer to these links :
SFTP Error Timeout after only 30 seconds but server limit is 600
Cannot connect via SSH (Failed to connect: Session operation has timed out)
Best Regards,
YongQing.
Tuesday, February 11, 2020 7:06 AM