0

I am trying to upload a file to an FTP server using C#. My code works right for simple txt files, but when I want to upload a video the uploaded file is corrupted.

This is my code for reading the file:

StreamReader sourceStream = new StreamReader(File.InputStream);
var content = sourceStream.ReadToEnd();

EDIT: i changed my code like this :

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(HostName + '/' + Folder + '/' + FileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(UserName, Password);
byte[] fileContent = new byte[File.ContentLength];
Stream ftpstream = request.GetRequestStream();
ftpstream.Write(fileContent, 0, File.ContentLength);
ftpstream.Close();
ftpstream.Flush();

here i'm not using streamReader,but still there is the problem

rzb
  • 29
  • 7
  • 1
    `Stream.ReadToEnd()` returns `string` ... that's why it's working with txt file – Selvin Dec 04 '18 at 19:09
  • 1
    Also see the [`StreamReader` documentation](https://docs.microsoft.com/en-us/dotnet/api/system.io.streamreader?view=netframework-4.7.2): *"StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file."* – user247702 Dec 04 '18 at 19:15
  • 1
    @Stijn - these might be a closer duplicate since `BinaryReader` isn't that useful here: [What is the purpose of StreamReader when Stream.Read() exists?](https://stackoverflow.com/q/27648464) and [FileStream vs/differences StreamWriter?](https://stackoverflow.com/q/4963667) – dbc Dec 04 '18 at 19:46
  • 1
    Have you tried this? [Upload and download a binary file to/from FTP server in C#/.NET](https://stackoverflow.com/q/44606028). You never actually fill `fileContent` with any contents so that's clearly wrong. – dbc Dec 04 '18 at 19:49
  • @dbc agree, I'll retract my close vote. – user247702 Dec 04 '18 at 19:50
  • thank you for your help.my problem solved by using `WebClient`.but i did't understand where was my problem .because when i filled my input stream the data was changing into another format .and i tried using every encodings but it still does not work. – rzb Dec 04 '18 at 20:15

0 Answers0