.NET Framework Bookmark and Share   
 index > .NET Framework Networking and Communication > broken file upload in ftp
 

broken file upload in ftp

hi
doing a project in ftp. while uploading a file when the network gets disconnected ,while resuming upload it must start from the place where the last byte was written im using ftpwebrequest class but it gives me error "File unavailable error code 550" below is my code.


if (System.IO.File.Exists(file_to_upload))

{

//FtpWebResponse response;

try

{

int Appendinglength = 0;

int TotalLength = 0;

pointer = 0;

long FTPFilesize = 0;

FileInfo finfo = new FileInfo(file_to_upload);

TotalLength =

Convert.ToInt32(finfo.Length);

FtpWebRequest ftprequest;

ftprequest = (

FtpWebRequest)FtpWebRequest.CreateDefault(new Uri("ftp://" + IP + "/" + finfo.Name));

ftprequest.Credentials =

new NetworkCredential(Uname, Pwd);

ftprequest.UseBinary =

true;

ftprequest.KeepAlive =

false;

//if (ISFtpFileExists(finfo.Name, out FTPFilesize))

//{

// //ftprequest.Method = WebRequestMethods.Ftp.AppendFile;

// ftprequest.Method = WebRequestMethods.Ftp.UploadFile;

//}

//else

//{

ftprequest.Method =

WebRequestMethods.Ftp.UploadFile;

//}

//ftprequest.ContentLength = finfo.Length - FTPFilesize;

ftprequest.UsePassive =

false;

ToWriteStream = ftprequest.GetRequestStream();

//response = (FtpWebResponse)ftprequest.GetResponse();

FileStream fst = new FileStream(file_to_upload, FileMode.Open);

fst.Seek(FTPFilesize,

SeekOrigin.Begin);

byte[] buffer = new byte[bufferLength];

pointer = fst.Read(buffer, 0, bufferLength);

while (pointer != 0)

{

ToWriteStream.Write(buffer, 0, pointer);

Appendinglength += pointer;

PrgUpdate(CalculateProgress(Appendinglength, TotalLength));

pointer = fst.Read(buffer, 0, bufferLength);

}

ToWriteStream.Close();

fst.Close();

}

catch (WebException ex)

{

FtpWebResponse response = ex.Response as FtpWebResponse;

Exoccur(response.StatusCode.ToString());

Exoccur(ex.Message);

//response.Close();

}

catch (Exception ex)

{

Exoccur(ex.Message);

return false;

}

finally

{

//ToWriteStream.Close();

////response.Close();

}

}

else

{

Exoccur(

"File Doesnt occur");

}

return true;

}

Kartheechidambaram
Following link will help you to upload file with resume support

http://www.codeproject.com/KB/IP/ftplib.aspx
Gaurav Khanna
Khanna Gaurav
it wont works,it contains code for upload only,not a broken upload
Kartheechidambaram

I found out the answer atlast it will works


public

bool UploadFile(string file_to_upload,int attempts)

{

if (System.IO.File.Exists(file_to_upload))

{

//FtpWebResponse response;

int Appendinglength = 0;

int TotalLength = 0;

pointer = 1;

long FTPFilesize = 0;

while (--attempts >= 0)

{

try

{

FileInfo finfo = new FileInfo(file_to_upload);

TotalLength =

Convert.ToInt32(finfo.Length);

FtpWebRequest ftprequest;

ftprequest = (

FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + IP + "/" + finfo.Name));

ftprequest.Credentials =

new NetworkCredential(Uname, Pwd);

ftprequest.UseBinary =

true;

ftprequest.KeepAlive =

false;

if (ISFtpFileExists(finfo.Name, out FTPFilesize))

{

ftprequest.Method =

WebRequestMethods.Ftp.AppendFile;

//ftprequest.ContentLength = finfo.Length - FTPFilesize;

//ftprequest.Method = WebRequestMethods.Ftp.UploadFile;

}

else

{

ftprequest.Method =

WebRequestMethods.Ftp.UploadFile;

}

ftprequest.UsePassive =

false;

ToWriteStream = ftprequest.GetRequestStream();

//response = (FtpWebResponse)ftprequest.GetResponse();

fstr =

new FileStream(file_to_upload, FileMode.Open);

fstr.Seek(FTPFilesize,

SeekOrigin.Begin);

byte[] buffer = new byte[bufferLength];

pointer = fstr.Read(buffer, 0, bufferLength);

startuptime =

DateTime.Now;

Exoccur(

"Resuming Upload..");

while (pointer != 0)

{

ToWriteStream.Write(buffer, 0, pointer);

Appendinglength += pointer;

int prg = CalculateProgress(Appendinglength, TotalLength);

PrgUpdate(prg);

//lblupdate(prg);

pointer = fstr.Read(buffer, 0, bufferLength);

}

break;

}

catch

{

}

}

enduptime =

DateTime.Now;

ToWriteStream.Close();

fstr.Close();

TimeSpan timetaken = enduptime.Subtract(startuptime);

double d = timetaken.TotalMilliseconds;

d = d / 1000;

Exoccur(

Convert.ToString(d));

}

else

{

Exoccur(

"File Doesnt occur");

}

return true;

}

Kartheechidambaram

You can use google to search for other answers

Custom Search

More Threads

• WCF UserName Authentication: must have a private key that is capable of key exchange
• c# upload method
• how to use datagrid view and pass values from textbox
• System.ServiceModel.CommunicationException:
• Exception: Unable to read data from the transport connection
• General Network Error
• Svcutil.exe issues: generates twice the same elements or generates codes that do not compile
• Iterate through files in a unc location
• WebClient.DownloadData times out
• 12157 ERROR_INTERNET_SECURITY_CHANNEL_ERROR on wininet function HttpSendRequestW