nezek
nezek

Reputation: 27

Cant Pass Hebrew in text file with FTP

I have a simple code that pass a text file to a FTP Server, the Text File is a simple Text "ANSI", Format - Windows-1255, with Hebrew inside.

When i Pass The File to the FTP Server And Download the file, the Hebrew character is turning to question mark (?), the file keeps its format ("ANSI", Format - Windows-1255).

Why is my Hebrew turning to question mark? (I'm working with .net4)

Here is My code

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpAddress + fileName);

request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(userName, password);

StreamReader sourceStream = new StreamReader(filePath);

byte[] fileContents = Encoding.GetEncoding(1255).GetBytes(sourceStream.ReadToEnd());

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

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();

Thank You

Upvotes: 0

Views: 333

Answers (1)

Antoine V
Antoine V

Reputation: 7204

I think the encoding of your file isn't 1255.

Try to open the file with encoding UTF-8 and recheck the result.

byte[] fileContents = Encoding.Default.GetBytes(sourceStream.ReadToEnd());

Or you can use a method Upload available in WebClient, so you even don't touch the file.

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
byte[] responseArray = myWebClient.UploadFile(ftpAddress + fileName, filePath);

Upvotes: 1

Related Questions