Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: sftp download yields corrupt files

$
0
0
I am trying to pull some files off a server. The files appear to download correctly without issue--they have the correct size and name. However none of them will open. When I look at any of the files in a text editor, they are full of null characters. I know it's not a problem with the files themselves, because I am able to pull them down manually and they open just fine.

Here's the relevant part of my C# code:
           using (SftpClient sftp = new SftpClient(hostname, username, password))
            {
                sftp.Connect();
                List<SftpFile> files = new List<SftpFile>();
                foreach (SftpFile sftpFile in sftp.ListDirectory(fileDir))
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        var asynchDownload = sftp.BeginDownloadFile(sftpFile.FullName, ms, null, null);
                        var sftpAsynch = asynchDownload as Renci.SshNet.Sftp.SftpDownloadAsyncResult;
                        while (!sftpAsynch.IsCompleted)
                        {
                            Console.Write(String.Format("\rDownloaded {0} of {1}.", sftpAsynch.DownloadedBytes, 0));
                        }
                        sftp.EndDownloadFile(asynchDownload);
                        using (FileStream file = new FileStream(stfpDestination + "\\" + sftpFile.Name, FileMode.Create, System.IO.FileAccess.Write))
                        {
                            byte[] bytes = new byte[ms.Length];
                            ms.Read(bytes, 0, (int)ms.Length);
                            file.Write(bytes, 0, bytes.Length);
                            ms.Close();
                        }
                    }
                }
                sftp.Disconnect();
            }
Thanks!

Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>