Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. FTP transfer corrupts .PNG files

FTP transfer corrupts .PNG files

Scheduled Pinned Locked Moved C#
sysadminhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anthasaurus
    wrote on last edited by
    #1

    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.

    N 1 Reply Last reply
    0
    • A anthasaurus

      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.

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2
      // 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

      A 1 Reply Last reply
      0
      • N Nagy Vilmos
        // 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

        A Offline
        A Offline
        anthasaurus
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups