ItsMods

Full Version: C# FTP upload image problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

First sorry if it's not the right section, but i didn't found a section with C#.
Second, as usual sorry for my english Wink

I making an application that needs to upload files, screens, etc to my team's server.
I can upload files like, .txt, .php etc but when i want to upload an image (jpg), the image on the server is not valid.
When i upload i got the message, 226 Transfert complete. So the upload seems to be ok... but not...

Example of the imagefile code before upload:
[Image: screenitsmods1.jpg]

Example of the imagefile code after upload:
[Image: screenitsmods2.jpg]

I upload files on ftp with this C# code:
CSHARP Code
  1. private void FtpUploadScreen(Uri uri, string fileName, Bitmap screen, ImageFormat fileFormat)
  2. {
  3. // Get the object used to communicate with the server.
  4. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri + "/" + fileName);
  5.  
  6. request.Method = WebRequestMethods.Ftp.UploadFile;
  7.  
  8. request.UseBinary = true;
  9. request.Credentials = new NetworkCredential(this.TB_FtpLogin.Text, this.FtpSecurePass(this.TB_FtpPass.Text));
  10. request.KeepAlive = false;
  11. request.UsePassive = false;
  12. request.Timeout = 10000;
  13.  
  14. screen.Save(this.AppDir + this.TempDir + "\\" + fileName, fileFormat);
  15.  
  16. StreamReader sourceStream = new StreamReader(this.AppDir + this.TempDir + "\\" + fileName);
  17.  
  18. byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
  19. sourceStream.Close();
  20. request.ContentLength = fileContents.Length;
  21.  
  22. Stream requestStream = request.GetRequestStream();
  23. requestStream.Write(fileContents, 0, fileContents.Length);
  24. requestStream.Close();
  25.  
  26. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  27.  
  28. //MessageBox.Show("Upload File Complete, status " + response.StatusDescription);
  29.  
  30. response.Close();
  31.  
  32. File.Delete(this.AppDir + this.TempDir + "\\" + fileName);
  33. }


I also tried to change "Encoding.UTF8.GetBytes(....)" whith Encoding.ASCII ... Encoding.Unicode... but nothing end with the same code after upload.
But if I try to upload a txt file with "test etc" as line 1, it uploads correctly and i got a txt file with "test etc" ...
What is wrong?

Thx in advance for your help.
I've had problems in the past with StreamWriter and StreamReader with files like this and never could figure out why. You can try to use a BinaryReader/Writer instead, but I'm still unsure if it will fix your problem
Hello,

@Nukem thank you for your answer, i'll try to use a BinaryReader/Writer instead.
@Nukem Thank you Big Grin

I used BinaryReader and it works fine now Wink

Here is the modified code of my 1st post: (if that can help someone)
CSHARP Code
  1. private void FtpUploadScreen(Uri uri, string fileName, Bitmap screen, ImageFormat fileFormat)
  2. {
  3. // Get the object used to communicate with the server.
  4. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri + "/" + fileName);
  5.  
  6. request.Method = WebRequestMethods.Ftp.UploadFile;
  7.  
  8. request.UseBinary = true;
  9. request.Credentials = new NetworkCredential(this.TB_FtpLogin.Text, this.FtpSecurePass(this.TB_FtpPass.Text));
  10. request.KeepAlive = false;
  11. request.UsePassive = false;
  12. request.Timeout = 10000;
  13.  
  14. screen.Save(this.AppDir + this.TempDir + "\\" + fileName, fileFormat);
  15.  
  16. FileStream fs = new FileStream(this.AppDir + this.TempDir + "\\" + fileName, FileMode.Open, FileAccess.Read);
  17. BinaryReader sourceStream = new BinaryReader(fs, new ASCIIEncoding());
  18.  
  19. Stream requestStream = request.GetRequestStream();
  20. byte[] chunk;
  21. chunk = sourceStream.ReadBytes(1024);
  22. int totalLength = 0;
  23. while(chunk.Length > 0)
  24. {
  25. requestStream.Write(chunk, 0, chunk.Length);
  26. totalLength += chunk.Length;
  27. chunk = sourceStream.ReadBytes(1024);
  28. }
  29. requestStream.Close();
  30. sourceStream.Close();
  31. request.ContentLength = totalLength;
  32.  
  33. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  34.  
  35. //MessageBox.Show("Upload File Complete, status " + response.StatusDescription);
  36.  
  37. response.Close();
  38.  
  39. File.Delete(this.AppDir + this.TempDir + "\\" + fileName);
  40. }


Thx!