• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help C# FTP upload image problem
#1
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.
  Reply
#2
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
[Image: b_560_95_1.png]
[-] The following 1 user says Thank You to Nukem for this post:
  • narkos
  Reply
#3
Hello,

@Nukem thank you for your answer, i'll try to use a BinaryReader/Writer instead.
  Reply
#4
@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!
  Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Help Android problem Yamato 12 2,508 04-25-2014, 04:49
Last Post: ScHmIdTy56789
  Problem with Rain Effects on Maps mitchhacker 5 1,289 10-22-2013, 00:46
Last Post: mitchhacker
  Help COD mw3 Error Couln't load image 3_cursor3:s Paylgs 2 5,176 07-18-2013, 18:46
Last Post: Nekochan
  Help Liberation Problem Yamato 27 14,081 07-17-2013, 19:54
Last Post: feature
  [xna 4]Rendering problem narkos 9 1,683 07-03-2013, 19:00
Last Post: Nekochan
  Help Problem God plugin v4.0 4nonymous 1 960 06-22-2013, 23:25
Last Post: 8q4s8
  Help Infected Shop Plugin Problem Hallla 2 1,281 05-06-2013, 18:29
Last Post: Hallla
  problem with gsc code CheGuevara 5 1,561 04-20-2013, 15:06
Last Post: Nekochan
  problem to connect to server s.j-rez 0 811 04-17-2013, 18:18
Last Post: s.j-rez
  Upload Clipboard Text data to a server DidUknowiPwn 14 2,446 04-13-2013, 14:48
Last Post: SuperNovaAO

Forum Jump:


Users browsing this thread: 1 Guest(s)