FTP transfer corrupts .PNG files
-
Hi, After transferring PNG images to an FTP server, I am unable to open them as they are being corrupted. My code is as below:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://... etc."); request.UsePassive = true; request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential("username", "password"); StreamReader sourceStream = new StreamReader(fileLocalDir); 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(); response.Close();
Another thing to add - when I download the file from the FTP server and open in IrfanView, the error below is displayed: "C:\Folder\file.png: Can't read file header! Unknown file format or file not found! (For unicode file names, please activate the Unicode PlugIn in 'Properties -> Languages')" Thanks for any assistance, Anthony.
-
Hi, After transferring PNG images to an FTP server, I am unable to open them as they are being corrupted. My code is as below:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://... etc."); request.UsePassive = true; request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential("username", "password"); StreamReader sourceStream = new StreamReader(fileLocalDir); 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(); response.Close();
Another thing to add - when I download the file from the FTP server and open in IrfanView, the error below is displayed: "C:\Folder\file.png: Can't read file header! Unknown file format or file not found! (For unicode file names, please activate the Unicode PlugIn in 'Properties -> Languages')" Thanks for any assistance, Anthony.
// Summary: // Implements a System.IO.TextReader that reads characters from a byte stream // in a particular encoding.
You're using the wrong type of stream. Maybe a
BinaryReader
would be better.
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H
-
// Summary: // Implements a System.IO.TextReader that reads characters from a byte stream // in a particular encoding.
You're using the wrong type of stream. Maybe a
BinaryReader
would be better.
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H
Thanks for your response Nagy. I have modified my code to use
BinaryReader
. I'm still able to transfer the files, but they end up being ~60% the size of the originals. I'm still unable to view the files due to corruption. Please can you advise whether my code is trying to do the right thing or not (see below)?FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftpuser...etc"); request.UsePassive = true; request.UseBinary = true; request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential("username", "password"); BinaryReader br = new BinaryReader(File.Open(fileLocalDir, FileMode.Open)); FileInfo info = new FileInfo(fileLocalDir); byte\[\] testArray = new byte\[info.Length\]; int i; for (i = 0; i < info.Length; i++) { testArray\[i\] = br.ReadByte(); } request.ContentLength = i; try { Stream requestStream = request.GetRequestStream(); requestStream.Write(testArray, 0, i); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); response.Close(); }
Regards, Anthony