Uploading files to FTP are corrupted once in destination

I'm creating a simple drag-file-and-upload-automatically-to-ftp windows application

and I'm using the MSDN code to upload the file to the FTP.

The code is pretty straight forward:

// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(String.Format("{0}{1}", FTP_PATH, filenameToUpload));
request.Method = WebRequestMethods.Ftp.UploadFile;

// Options
request.UseBinary = true;
request.UsePassive = false;

// FTP Credentials
request.Credentials = new NetworkCredential(FTP_USR, FTP_PWD);

// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(fileToUpload.FullName);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
request.ContentLength = fileContents.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
writeOutput("Upload File Complete!");
writeOutput("Status: " + response.StatusDescription);


and it does get uploaded to the FTP

Problem is when I see the file on a browser, or simply download and try to see it on desktop I get:

I already used request.UseBinary = false; and request.UsePassive = false; but it does not seam to do any kind of good whatsoever.

What I have found out was that, the original file has 122Kb lenght and in the FTP (and after downloading), it has 219Kb...

What am I doing wrong?

By the way, the uploadFileToFTP() method is running inside a BackgroundWorker, but I don't really thing that makes any difference...


You shouldn't use a StreamReader but only a Stream to read binary files.

Streamreader is designed to read text files only.

Try with this :

private static void up(string sourceFile, string targetFile)
        string ftpServerIP = ConfigurationManager.AppSettings["ftpIP"];
        string ftpUserID = ConfigurationManager.AppSettings["ftpUser"];
        string ftpPassword = ConfigurationManager.AppSettings["ftpPass"];
        ////string ftpURI = "";
        string filename = "ftp://" + ftpServerIP + "//" + targetFile; 
        FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(filename);
        ftpReq.UseBinary = true;
        ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
        ftpReq.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

        byte[] b = File.ReadAllBytes(sourceFile);

        ftpReq.ContentLength = b.Length;
        using (Stream s = ftpReq.GetRequestStream())
            s.Write(b, 0, b.Length);

        FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse();

        if (ftpResp != null)
    catch (Exception ex)

The problems are caused by your code decoding the binary data to character data and back to binary data. Don't do this.

Use the UploadFile Method of the WebClient Class:

using (WebClient client = new WebClient())
    client.Credentials = new NetworkCredential(FTP_USR, FTP_PWD);
    client.UploadFile(FTP_PATH + filenameToUpload, filenameToUpload);

Need Your Help

Yii , show different date format in cgridview

php yii

I had following cgridview in Yii application,

How to store a secret API key in an application's binary?

twitter binary api-key secret-key

I am creating a Twitter client for Mac OS X and I have a Consumer secret. It's to my understanding I should not share this secret key. The problem is that when I put it as a string literal into my