Share via


How to upload text into a file on an FTP server

Question

Saturday, November 17, 2018 12:03 AM

Upload some txt files or idk files

All replies (4)

Saturday, November 17, 2018 5:07 PM

This thread could be useful for you: https://social.msdn.microsoft.com/Forums/cs-CZ/ebe27606-d767-4289-bebb-2708a2f38251/problem-error?forum=csharpgeneral


Monday, November 19, 2018 5:28 AM

Hi Ahron321,

If you want to upload text file or other file into ftp server, please refer the following code.

string ftpUsername = "";
            string ftpPassword = "";
            string localFilePath = "";
            using (WebClient client = new WebClient())
            {
                client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
                client.UploadFile("ftp://ftpserver.com/text.txt", WebRequestMethods.Ftp.UploadFile, localFilePath);
            }

If you want to Upload text into a file on an FTP server in C#, please refer to the following code. 

private void FtpUploadTxt(string textContent, string ftpUrl,
    string userName, string password)
{
    
    FtpWebRequest request =
        (FtpWebRequest)WebRequest.Create(ftpUrl);
    request.Method = WebRequestMethods.Ftp.UploadFile;

    // Get network credentials.
    request.Credentials =
        new NetworkCredential(userName, password);

    // Write the textContent's bytes into the request stream.
    request.ContentLength = textContent.Length;
    using (Stream request_stream = request.GetRequestStream())
    {
        byte[] bytes = Encoding.UTF8.GetBytes(textContent);
        request_stream.Write(bytes, 0, textContent.Length);
        request_stream.Close();
    }
}

Best regards,

Zhanglong 

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Monday, November 19, 2018 2:56 PM


Tuesday, November 20, 2018 8:06 AM

Hi Ahron,

The code sample that I mentioned all work on my side, could you please share the detailed error message in English.

Best regards,

Zhanglong

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].