I am trying to upload files on ftp with resume if internet connection is lost. It does resume file uploading 2-3 times if internet connection is lost but after that an exception is thrown that says "Server committed a protocol violation". Please review code given below and help me if you find any issues in code. I know the connection remains open somewhere in the code but I am unable to identify the place.
protected void upload_file(String server_path, string file_path)
{
if (reqFTP != null)
reqFTP.Abort();
bool flag = false;
FileInfo fileInf = new FileInfo(file_path);
String file_name = fileInf.Name;
String ftpusername = "aaaa";
String ftppassword = "Aas123";
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp" + file_name));
reqFTP.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
reqFTP.KeepAlive = false;
FtpWebResponse response;
try
{
long size = 0;
using (response = (FtpWebResponse)reqFTP.GetResponse())
{
size = response.ContentLength;
}
response.Close();
reqFTP.Abort();
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp" + file_name));
reqFTP.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
reqFTP.Method = System.Net.WebRequestMethods.Ftp.AppendFile;
reqFTP.KeepAlive = false;
int b = 0;
System.IO.FileInfo fi = new System.IO.FileInfo(file_path);
int bytes = 0;
System.IO.FileStream fs = new FileStream(file_path, FileMode.Open, FileAccess.Read);
fs.Seek(size, SeekOrigin.Begin);
long total_bytes = fi.Length - size;
byte[] buffer = new byte[8092];
using (System.IO.Stream rs = reqFTP.GetRequestStream())
{
while ((bytes = fs.Read(buffer, 0, buffer.Length)) != 0)
{
try
{
rs.Write(buffer, 0, bytes);
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
while (!checkForInternetConnection()) ;
upload_file(server_path, file_path);
}
}
}
fs.Close();
using (FtpWebResponse uploadResponse = (FtpWebResponse)reqFTP.GetResponse())
{
var value = uploadResponse.StatusDescription;
MessageBox.Show(value);
uploadResponse.Close();
}
}
catch (WebException ex)
{
MessageBox.Show(ex.Message);
while (!checkForInternetConnection()) ;
FtpStatusCode status;
using (response = (FtpWebResponse)ex.Response)
{
status = (FtpStatusCode)response.StatusCode;
}
if (status == FtpStatusCode.ActionNotTakenFileUnavailable)
{
MessageBox.Show("file not found");
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp" + file_name));
reqFTP.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
reqFTP.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
reqFTP.KeepAlive = false;
int b = 0;
System.IO.FileInfo fi = new System.IO.FileInfo(file_path);
int bytes = 0;
System.IO.FileStream fs = new FileStream(file_path, FileMode.Open, FileAccess.Read);
long total_bytes = fi.Length;
byte[] buffer = new byte[8092];
using (System.IO.Stream rs = reqFTP.GetRequestStream())
{
while ((bytes = fs.Read(buffer, 0, buffer.Length)) != 0)
{
try
{
rs.Write(buffer, 0, bytes);
}
catch
{
while (!checkForInternetConnection()) ;
upload_file(server_path, file_path);
}
}
}
fs.Close();
using (FtpWebResponse uploadResponse = (FtpWebResponse)reqFTP.GetResponse())
{
var value = uploadResponse.StatusDescription;
MessageBox.Show(value);
}
}
catch (WebException ex1)
{
MessageBox.Show("catch it");
while (!checkForInternetConnection()) ;
upload_file(server_path, file_path);
}
}
else
{
while (!checkForInternetConnection()) ;
upload_file(server_path, file_path);
}
}
}
I have tried
reqFTP.KeepAlive = true
but that does not work.