Saving a string in a txt file on an FTP server

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 fine.

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

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

+6
source share
1 answer

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

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

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

However, you can shorten this by simply turning the string into a byte array , avoiding the need to make Stream as a whole:

 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(); 
+7
source

Source: https://habr.com/ru/post/955719/


All Articles