0

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

Alex K.
  • 171,639
  • 30
  • 264
  • 288
nezek
  • 21
  • 1
  • 4
  • Why does the encoding matter here? Why not just File.ReadAllBytes() and upload that for an exact binary copy. – Alex K. Aug 29 '18 at 15:41

1 Answers1

1

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);
Antoine V
  • 6,998
  • 2
  • 11
  • 34
  • +1 - Though I just want to clarify that it's not `FtpWebRequest` vs. `WebClient`. Both can transfer the file as is (binary transfer) and vice versa (actually `WebClient` uses `FtpWebRequest` internally). It's just that OP's code modified the file (and incorrectly), while the `WebClient` code in this answer transfers the file intact. See also [Upload and download a binary file to/from FTP server in C#/.NET](https://stackoverflow.com/q/44606028/850848). – Martin Prikryl Aug 29 '18 at 15:51