Click here to Skip to main content
15,919,434 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to upload a file to a ftp server from my application.
But when upload completes, the file uploaded is bigger than the original one
I have discovered that if I have of buffer of 1024 bytes and on the last turn only 512 are occupied, it also writes the rest "0" 512 bytes to the file

Here is my code:

C#
public bool UploadArticol(String adresaserver,String numefisier)
        {
            try
            {
                FileInfo toupload = new FileInfo("articles\\"+numefisier+".dat");
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://"+adresaserver+"/" + toupload.Name);
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential("lucian", "lucian");
                Stream ftpstream = request.GetRequestStream();
                FileStream file = File.OpenRead("articles\\"+numefisier+".dat");
                int length = 1048576;
                byte[] buffer = new byte[length];
                int bytesread = 0;
                do
                {
                    bytesread = file.Read(buffer, 0, length);
                    ftpstream.Write(buffer, 0, length);
                }
                while (bytesread != 0);
                file.Flush();
                file.Close();
                ftpstream.Close();
               Console.WriteLine("Upload complete");
               
            }
            catch (Exception ee)
            {
               Console.WriteLine(ee.Message);
               return false;
            }
            return true;
        }
Posted

You are always writing the data in blocks of 1048576 bytes no matter how many are actually read.
Two choices:
1. Change the Write() to be:
C#
ftpstream.Write(buffer, 0, bytesread);


2. Just have the two streams deal with each other directly:
C#
public bool UploadArticol(String adresaserver, String numefisier)
{
  string uploadname = "articles\\"+numefisier+".dat";
  try
  {
    FileInfo toupload = new FileInfo(uploadname);
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://"+adresaserver+"/" + toupload.Name);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Credentials = new NetworkCredential("lucian", "lucian");
    Stream ftpstream = request.GetRequestStream();
    using (FileStream file = File.OpenRead(uploadname))
    {
      file.CopyTo(ftpstream);
    }
    Console.WriteLine("Upload complete");
  }
  catch (Exception ee)
  {
    Console.WriteLine(ee.Message);
    return false;
  }
  return true;
}
 
Share this answer
 
Comments
Luci C 5-Apr-13 16:32pm    
you are a genius! thank you so much for the first simple solution...
how couldn't i remark that?
Again thank you
Matt T Heffron 5-Apr-13 17:29pm    
You're welcome.
The second solution is likely better, as the buffering size is handled by the streams without you assuming a specific buffer size. And it is less code.
you may need to use Flush() method on stream object as well...
if you have still issues, refer these links..
http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Upload-file-to-FTP-Server.html[^]
http://blog.pbdesk.com/2009/03/uploading-files-to-ftp-server-using.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900