0

Hello there fellow programmers! I have an issue with sending PDF through FTP. I want to copy propely created PDF to FTP directory, unfortunately file i send through has proper size but i cannot open it with any PDF editor. I've tried searching soulutions but my converting and encoding does not seem to work. Here is my code:

     public string SendPDF(string FileNamePath, string ShortFileName)
        {
            string Response = string.Empty;
          //FullPath is generated in class constructor
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FullPath+@"\"+ShortFileName+".pdf");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            request.Credentials = new NetworkCredential(User, Password);
       //FileNamePath is directory of source file
           byte[] fileContents= File.ReadAllBytes(@FileNamePath + ".pdf");
            string pdfBase64 = Convert.ToBase64String(fileContents);

            using(var stream  = GenerateStreamFromString(pdfBase64))
            {
                request.ContentLength = stream.Length;

                using (Stream requestStream = request.GetRequestStream())
                {
                    stream.CopyTo(requestStream);
                    requestStream.Close();
                }
            }

                



            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                Response = "Upload File PDF Complete, status" + Convert.ToString(response.StatusDescription);
            }

            return Response;
        }
 public static Stream GenerateStreamFromString(string s)
        {
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);
            writer.Write(s);
            writer.Flush();
            stream.Position = 0;
            return stream;
        }

Also, I've tried code below but it has the same issue:

 byte[] fileContents ;

            using (StreamReader sourceStream = new StreamReader(FileNamePath + ".pdf"))
            {
                fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            }

            request.ContentLength = fileContents.Length;

            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(fileContents, 0, fileContents.Length);
            }

Sending through XML file works completely fine. Thank you in advance and please don't be harsh this is my first question here :3

  • 1
    Why do you send Base64 strings of the bytes? You should directly send the raw bytes of the PDF files to the server side. – Lex Li Aug 09 '21 at 17:56
  • Indeed, neither of your code can safely upload a binary file. Not to mention that the first code is terrible. For a correct code, see [Upload and download a file to/from FTP server in C#/.NET](https://stackoverflow.com/q/44606028/850848). – Martin Prikryl Aug 09 '21 at 19:15
  • Unfortunately none of solutions posted there does not solve my problem :(. – serghio Aug 10 '21 at 09:16
  • @LexLi i thought it may help to change the encoding of pdf file. When i send raw bytes like in my second code it still does not work. – serghio Aug 10 '21 at 09:31

1 Answers1

0

I found the solution. The issue was lack of parameter request.UseBinary = true. Here is proper code:

public string SendPDF(string FileNamePath, string ShortFileName)
        {
            string Response = string.Empty;
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPPath+@"\"+ShortFileName+".pdf");
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.UseBinary = true;
           
            request.Credentials = new NetworkCredential(User, Password);
          
            byte[] fileContents;

            using (StreamReader sourceStream = new StreamReader(FileNamePath + ".pdf"))
            {
                fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            }

            request.ContentLength = fileContents.Length;

            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(fileContents, 0, fileContents.Length);
            }


            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                Response = "Upload File PDF Complete, status" + Convert.ToString(response.StatusDescription);
            }

            return Response;
        }