This project is read-only.

Downloading File to MemoryStream

Feb 23, 2012 at 6:59 PM

Hi,

I'm trying to download an XML file directly into a MemoryStream so that I can parse it in memory. My code looks like this:

 

var ftpClient = new SftpClient(connectionInfo)
using (var strm = new MemoryStream())
{
	ftpClient.DownloadFile(ftpFile.FullName, strm);

	.
	.
	.

	strm.Close();
}

 

I kept getting errors about not finding a root element. To troubleshoot I had the MemoryStream write to a file. When I opened that file it was full of NULL characters. Downloading a file into a FileStream gets me the correct file.

What am I doing wrong?

Thanks!

Coordinator
Feb 24, 2012 at 6:35 PM

Hi,

 

I just tested it with this code and everything worked fine:

            using (var sftp = new SftpClient(connectionInfo))
            {
                sftp.Connect();
                using(var mem = new MemoryStream())
                {
                    sftp.DownloadFile("data1.xml", mem);
                    var output = ASCIIEncoding.ASCII.GetString(mem.ToArray());
                    Console.WriteLine(output);
                }
                sftp.Disconnect();
            }

Please try this and let me know if it works for you.

 

Thanks,

Oleg

Feb 24, 2012 at 7:28 PM

That worked! Thanks a lot!