Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a string to a txt file on an FTP server

Tags:

json

c#

ftp

I am trying to save a string containing Json syntax to a .txt file on an FTP server. I tried using this example http://msdn.microsoft.com/en-us/library/ms229715.aspx which worked great.

But this example takes an existing .txt local file and uploads it to the ftp server.

I would like to directly create / update a txt file on the ftp server from a string variable. Without having first to create the txt file locally in my pc.

like image 464
jayt.dev Avatar asked Oct 10 '13 22:10

jayt.dev


1 Answers

Your example link is exactly what you need, but you need to get your information from a MemoryStream instead of an existing file.

You can turn a string directly into a Stream with this:

MemoryStream memStr = MemoryStream(UTF8Encoding.Default.GetBytes("asdf"));

However, you can shortcut this more by directly turning your string into a byte array, avoiding the need to make a Stream altogether:

System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(yourString);

//and now plug that into your example
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
like image 54
gunr2171 Avatar answered Oct 28 '22 16:10

gunr2171