brawl_up
brawl_up

Reputation: 85

File corrupted after upload to FTP server with WebRequest

Here's some problem with transporting .csv file to FTP server. Before transfering it's okay, but when i'm checking it on FTP it looks like broken or something:

"ЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ  T8пїЅпїЅпїЅпїЅпїЅпїЅ\p3>@L @E8?5=:>                                                                               BпїЅaпїЅ="

Is it problem with encoding? I'm using this method of download:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxxxxxxx.xx/" + name);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.KeepAlive = true;
        request.UseBinary = true;
        request.Credentials = new NetworkCredential("xxxx", "qweqwe123");
        StreamReader sourceStream = new StreamReader("F:\\" + xxxx);

        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();
        response.Close();

Upvotes: 1

Views: 703

Answers (2)

Chris Catignani
Chris Catignani

Reputation: 5306

I can across this question with the same issue. Expanding on Brawl_Ups solution... And it seems that its best to use the BinaryReader for binary files. This is a snippet I eventfully came up with... This is a current work in progress and any edits are welcome.

public string UploadFile()
{
    string sRetVal = string.Empty;
    string sFullDestination = this.DestinatinFullIDPath + this.UpLoadFileName;
    try
    {
        if ((this.CreateDestinationDir(this.DestinatinFullModulePath) == true) &
            (this.CreateDestinationDir(this.DestinatinFullIDPath) == true))
        {

            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(sFullDestination);
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpRequest.Credentials = new NetworkCredential(this.UserName, this.Password);
            ftpRequest.UsePassive = true;
            ftpRequest.UseBinary = true;
            ftpRequest.EnableSsl = false;

            byte[] fileContents;
            using (BinaryReader binReader = new BinaryReader(File.OpenRead(this.UpLoadFullName)))
            {
                FileInfo fi = new FileInfo(this.UpLoadFullName);
                binReader.BaseStream.Position = 0;
                fileContents = binReader.ReadBytes((int)fi.Length);
            }

            ftpRequest.ContentLength = fileContents.Length;
            using (Stream requestStream = ftpRequest.GetRequestStream())
            {
                requestStream.Write(fileContents, 0, fileContents.Length);
            }

            using (FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse())
            {
                sRetVal = string.Format("Upload File Complete, status {0}", response.StatusDescription);
            }
        }
    }
    catch (WebException ex)
    {
        throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
    }
    return sRetVal;
}

Upvotes: 0

brawl_up
brawl_up

Reputation: 85

Don't like to answer under my aquestions, but it's too long for comment: Solved, maybe somebody have this problem. Try to use this upload method:

FileInfo toUpload = new FileInfo("log.txt");
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://csharpcoderr.com/public_html/" + toUpload.Name);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("name", "pass");
Stream ftpStream = request.GetRequestStream();
FileStream fileStream = File.OpenRead("log.txt");
byte[] buffer = new byte[1024];
int bytesRead = 0;
do
{
   bytesRead = fileStream.Read(buffer, 0, 1024);
   ftpStream.Write(buffer, 0, bytesRead);
}
while (bytesRead != 0);
fileStream.Close();
ftpStream.Close();

Upvotes: 1

Related Questions