7

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.

1
  • Unless you have direct access to the file system of the FTP server, the only way to do this is to upload a new file. Commented Oct 10, 2013 at 22:24

1 Answer 1

9

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.