Answer the question
In order to leave comments, you need to log in
Why doesn't uploading a file to an FTP server using C# work?
I'm trying to upload a file to an FTP server using C#, but I get the error "The underlying connection was closed: The server violated the protocol." A Google search brought up the same error, where the following code helped the person, but I still get this error.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
namespace SteamSSFN
{
class Program
{
static void Main()
{
ftpfile("/file.txt", "C:\\file.txt");
}
public static void ftpfile(string ftpfilepath, string inputfilepath)
{
string ftphost = "ip:port";
string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential("user", "pass");
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Proxy = null;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(inputfilepath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
}
}
Answer the question
In order to leave comments, you need to log in
Brr, you first look with a sniffer what kind of packets are coming.
And yes, it's better to use webclient
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question