Allan Rwakatungu
Allan Rwakatungu

Reputation: 1107

Upload FTP file to remote site from memory using C#

Am working on a project that requires uploading xml file to remote FTP site.

Is it possible to save xml string from memory to remote FTP site? ... from what i see i have to first write the file to local disk then read from disk and FTP to remote site.

I am using c#.

Thank you.

Upvotes: 2

Views: 4969

Answers (1)

TimothyP
TimothyP

Reputation: 21745

It's perfectly possible to use a MemoryStream instead of a FileStream to "write" data to an FTP server.

From the top of my head: (just a snippet of code, I asume you have the FTP stuff already)

var data = ASCIIEncoding.ASCII.GetBytes(yourXmlString);
using (var dataStream = new MemoryStream(data))
using (var requestStream = ftpRequest.GetRequestStream())
{
     contentLength = dataStream.Read(buffer, 0, bufferLength);

     while (contentLength != 0)
     {
          requestStream.Write(buffer,0,bufferLength);
          contentLength = dataStream.Read(buffer, 0, bufferLength);
     }
}

In other words, you simply need a stream, doesn't matter if it's a FileStream or MemoryStream

Upvotes: 3

Related Questions