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:
Code:
private void FtpUploadScreen(Uri uri, string fileName, Bitmap screen, ImageFormat fileFormat) { // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri + "/" + fileName); request.Method = WebRequestMethods.Ftp.UploadFile; request.UseBinary = true; request.Credentials = new NetworkCredential(this.TB_FtpLogin.Text, this.FtpSecurePass(this.TB_FtpPass.Text)); request.KeepAlive = false; request.UsePassive = false; request.Timeout = 10000; screen.Save(this.AppDir + this.TempDir + "\\" + fileName, fileFormat); StreamReader sourceStream = new StreamReader(this.AppDir + this.TempDir + "\\" + fileName); 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(); //MessageBox.Show("Upload File Complete, status " + response.StatusDescription); response.Close(); File.Delete(this.AppDir + this.TempDir + "\\" + fileName); }

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]
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)
Code:
private void FtpUploadScreen(Uri uri, string fileName, Bitmap screen, ImageFormat fileFormat) { // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri + "/" + fileName); request.Method = WebRequestMethods.Ftp.UploadFile; request.UseBinary = true; request.Credentials = new NetworkCredential(this.TB_FtpLogin.Text, this.FtpSecurePass(this.TB_FtpPass.Text)); request.KeepAlive = false; request.UsePassive = false; request.Timeout = 10000; screen.Save(this.AppDir + this.TempDir + "\\" + fileName, fileFormat); FileStream fs = new FileStream(this.AppDir + this.TempDir + "\\" + fileName, FileMode.Open, FileAccess.Read); BinaryReader sourceStream = new BinaryReader(fs, new ASCIIEncoding()); Stream requestStream = request.GetRequestStream(); byte[] chunk; chunk = sourceStream.ReadBytes(1024); int totalLength = 0; while(chunk.Length > 0) { requestStream.Write(chunk, 0, chunk.Length); totalLength += chunk.Length; chunk = sourceStream.ReadBytes(1024); } requestStream.Close(); sourceStream.Close(); request.ContentLength = totalLength; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //MessageBox.Show("Upload File Complete, status " + response.StatusDescription); response.Close(); File.Delete(this.AppDir + this.TempDir + "\\" + fileName); }

Thx!
Reply



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

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2026 Melroy van den Berg.