Problem while uploading file on FTP server
-
Hi All, I am working on FTP uitility using C#. I have build a class for that using C#. But while uploading the file on FTP i am unable to upload whole file on the FTP. Only part of that file is uploaded. I have used the following code to upload the file. -------------------------------------------------------------------------- public void UploadData(string FTPUri, string FilePath, string FileName, string UserName, string Password) { FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(FTPUri+FileName); reqFTP.Method = WebRequestMethods.Ftp.UploadFile; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(UserName, Password); StreamReader ReadStream = new StreamReader(FilePath); string ftpResponse = ReadStream.ReadToEnd(); ReadStream.Close(); reqFTP.ContentLength = ftpResponse.Length; StreamWriter ftpStream = new StreamWriter(reqFTP.GetRequestStream()); ftpStream.WriteLine(ftpResponse); ftpStream.Close(); ftpStream.Close(); } -------------------------------------------------------------------------- Here i am able to upload only part of that file. Can anybody tell me what is the problem? Thanking you.
Pravin
-
Hi All, I am working on FTP uitility using C#. I have build a class for that using C#. But while uploading the file on FTP i am unable to upload whole file on the FTP. Only part of that file is uploaded. I have used the following code to upload the file. -------------------------------------------------------------------------- public void UploadData(string FTPUri, string FilePath, string FileName, string UserName, string Password) { FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(FTPUri+FileName); reqFTP.Method = WebRequestMethods.Ftp.UploadFile; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(UserName, Password); StreamReader ReadStream = new StreamReader(FilePath); string ftpResponse = ReadStream.ReadToEnd(); ReadStream.Close(); reqFTP.ContentLength = ftpResponse.Length; StreamWriter ftpStream = new StreamWriter(reqFTP.GetRequestStream()); ftpStream.WriteLine(ftpResponse); ftpStream.Close(); ftpStream.Close(); } -------------------------------------------------------------------------- Here i am able to upload only part of that file. Can anybody tell me what is the problem? Thanking you.
Pravin
for getting full data without error use binary reader for uploading like this //FtpWebReq.Credentials = new NetworkCredential(userId, Password); // FtpWebReq.Method = WebRequestMethods.Ftp.UploadFile; //FtpWebReq.KeepAlive = false; // FtpWebReq.UseBinary = true; //FtpWebReq.ContentLength = FINFO.Length; FileStream fsSource = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader binReader = new BinaryReader(fsSource); Stream strm=null; byte[] byteRead; binReader.BaseStream.Seek(0, SeekOrigin.Begin); long pos = 0, fLen = 0; int buffLength = 2048; int readSize = buffLength; fLen = FINFO.Length; try { strm = FtpWebReq.GetRequestStream(); while (binReader.BaseStream.Position < fLen) { pos = binReader.BaseStream.Position; if (fLen - pos < buffLength) readSize = (int)(fLen - pos); byteRead = binReader.ReadBytes(readSize); strm.Write(byteRead, 0, readSize); } binReader.Close(); strm.Close(); fsSource.Close(); }