Uploading files to FTP are corrupted once in destination

balexandre picture balexandre · Apr 26, 2012 · Viewed 10.5k times · Source

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

enter image description here

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());
sourceStream.Close();
request.ContentLength = fileContents.Length;

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

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

response.Close();

and it does get uploaded to the FTP

enter image description here

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

enter image description here

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...

Answer

LaGrandMere picture LaGrandMere · Apr 26, 2012

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)
{            
    try
    {
        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)
        {
            MessageBox.Show(ftpResp.StatusDescription);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}