12

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

balexandre
  • 73,608
  • 45
  • 233
  • 342
  • Binary data and UTF-8 doesn't mix well. – dtb Apr 26 '12 at 09:28
  • @dtb using `Encoding.ASCII.GetBytes(sourceStream.ReadToEnd());` gets the same behavior... – balexandre Apr 26 '12 at 09:30
  • Sigh. **Binary** data and **character** data are two different things. Yes, you can encode characters to bytes, but you can not blindly decode bytes that do not encode characters. – dtb Apr 26 '12 at 09:32
  • Instead of StreamReader, you should probably try [BinaryReader](http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx). – Jiaji Wu Apr 26 '12 at 09:35
  • found out a near duplicated http://stackoverflow.com/questions/8495064/ftp-upload-is-too-big – balexandre Apr 26 '12 at 12:08

2 Answers2

33

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());
    }
}
LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
3

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);
}
Liquid Core
  • 1
  • 6
  • 27
  • 52
dtb
  • 213,145
  • 36
  • 401
  • 431