Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem while uploading file on FTP server

Problem while uploading file on FTP server

Scheduled Pinned Locked Moved C#
questioncsharpsysadminhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pavya_Cool
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • P pavya_Cool

      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

      J Offline
      J Offline
      JABIR E
      wrote on last edited by
      #2

      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(); }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups