Search code examples
c#filefile-uploadftppgp

FTP Changing PGP File During Transfer in C#


I have PGP files that I've verified as being valid, but at some point during the FTP upload, they become corrupt. When retrieved, I get an error message stating "Found no PGP information in these file(s)."

For what it's worth, the PGP is version 6.5.8, but I think that this is unimportant, as the files seem alright before they're uploaded.

My code is as follows for the file transfer, is there a setting or field that I've missed?

static void FTPUpload(string file)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.itginc.com" + "/" + Path.GetFileName(file));

        request.UseBinary = true;
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(ApplicationSettings["Username"], ApplicationSettings["Password"]);

        StreamReader sr = new StreamReader(file);

        byte[] fileContents = Encoding.UTF8.GetBytes(sr.ReadToEnd());
        sr.Close();

        request.ContentLength = fileContents.Length;

        Stream requestStream = request.GetRequestStream();

        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();

        FtpWebResponse resp = (FtpWebResponse)request.GetResponse();

        Console.WriteLine("Upload file complete, status {0}", resp.StatusDescription);

        resp.Close();
        string[] filePaths= Directory.GetFiles(tempPath);
        foreach (string filePath in filePaths) 
            File.Delete(filePath);
    }

Any help is appreciated


Solution

  • Hmmmm try not reading it into a byte array and instead doing something like this

            using (var reader = File.Open(source, FileMode.Open))
            {
                var ftpStream = request.GetRequestStream();
                reader.CopyTo(ftpStream);
                ftpStream.Close();
            }