mike Delvotti

mike Delvotti

  • NA
  • 262
  • 0

Convert file to byte from text

Mar 26 2012 10:35 AM
Guys i'm a little stuck on the below, it works fine for uploading text files, but really i need it to upload jpg/jpeg/png is there a simple way to adapt somewhere below to upload images?

At the moment it places a jpg in the destination folder, but the file is not viewable or contains no information?

// Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftpserver" + "/" + label1.Text + "" + "Winter.jpg");
                request.Method = WebRequestMethods.Ftp.UploadFile;
                //Path.GetFileName(filePath));
                // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential("username", "password");


                // Copy the contents of the file to the request stream.
                StreamReader sourceStream = new StreamReader(@"C:\\Winter.jpg");

                I feel my problem is here somewhere?
                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();

                Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

                response.Close();


Answers (7)