Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading files to FTP are corrupted once in destination

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

like image 289
balexandre Avatar asked Apr 26 '12 09:04

balexandre


2 Answers

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());
    }
}
like image 194
LaGrandMere Avatar answered Sep 24 '22 14:09

LaGrandMere


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);
}
like image 22
dtb Avatar answered Sep 24 '22 14:09

dtb