I'm trying to upload files to an FTP, and have been doing so successfully until today. Here (at work) I've never had issues, but in production on-site we have had sporadic issues, but now today all of a sudden it wont work 100% of the time and I can't figure this out for the life of me.
I've tried nearly everything I could find.
Below is the method I use. Let me know if you need anymore information.
private static void FtpUpload(String username, String password, String address,
Int32 port, Boolean usePassive, String filePath)
{
try
{
String fileName = "";
fileName = Path.GetFileName(filePath);
FtpWebRequest request = null;
request = (FtpWebRequest)FtpWebRequest.Create(String.Format("ftp://{0}", address));
request.Credentials = new NetworkCredential(
username,
password);
request.UsePassive = usePassive;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Timeout = -1;
request.ReadWriteTimeout = -1;
request.KeepAlive = false;
request.Proxy = null;
Console.WriteLine(String.Format("Uploading {0}...", fileName));
Stream ftpStream = null;
using (ftpStream = request.GetRequestStream())
{
ftpStream.WriteTimeout = -1;
ftpStream.ReadTimeout = -1;
using (FileStream file = File.OpenRead(filePath))
{
file.CopyTo(ftpStream);
}
}
}
catch
{
throw;
}
}
EDIT:
This code snippit works. Note when I update this snippet to use using blocks it goes back to failing. Could the use of using block be the cause?
public static void FtpUpload(
String username,
String password,
String address,
Int32 port,
Boolean usePassive,
String filePath)
{
string ftpServerIP = String.Format("ftp://{0}", address);
string ftpUserID = username;
string ftpPassword = password;
FileInfo fileInf = new FileInfo(filePath);
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerIP);
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = false;
request.UsePassive = false;
request.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
Stream strm = request.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch
{
throw;
}
}
For anyone else who stumbles across this, the solution contained in
http://www.experts-exchange.com/questions/28693905/FtpWebRequest-The-underlying-connection-as-closed-An-unexpected-error-has-occurred-on-a-receive.html
worked for me, you had to set the following to false
var request = (FtpWebRequest)WebRequest.Create(finalPath);
request.UseBinary = false;
request.UsePassive = false;
This solved the problem of the FTP upload working locally and failing once deployed to UAT
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With