I want to upload a Windows local drive file to a Linux server FTP.

patrick

Well-known member
Joined
Dec 5, 2021
Messages
251
Programming Experience
1-3
I want to upload a Windows local drive file to a Linux server FTP.
Do it in C#.
Is it correct to enter this in this part?

string FTP_Linux_Path = "FTP://201.50.215.32/test1"; <==== Linux Server FTP
string FTP_Linux_user = "LinuxUser"; <==== Linux Server FTP
string FTP_Linux_pwd = "LinuxPwd"; <==== Linux Server FTP
string inputFile = "D:\\test1.txt"; <=== Windows Local Drive Folder



C#:
 public static void FtpUpload()
        {
            string FTP_Linux_Path = "FTP://201.50.215.32/test1"; <==== Linux Server FTP
            string FTP_Linux_user = "LinuxUser"; <==== Linux Server FTP
            string FTP_Linux_pwd = "LinuxPwd"; <==== Linux Server FTP
            string inputFile = "D:\\test1.txt"; <=== Windows Local Drive Folder


            FtpWebRequest req = (FtpWebRequest)WebRequest.Create(ftpPath);

            req.Method = WebRequestMethods.Ftp.UploadFile;

            req.Credentials = new NetworkCredential(user, pwd);


            byte[] data;
            using (StreamReader reader = new StreamReader(inputFile))
            {
                data = Encoding.UTF8.GetBytes(reader.ReadToEnd());
            }


            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
            }


            using (FtpWebResponse resp = (FtpWebResponse)req.GetResponse())
            {
                Console.WriteLine("Upload: {0}", resp.StatusDescription);
            }
        }
 
An FTP server is an FTP server. The OS it's running on is irrelevant because you're dealing only with the FTP protocol. It's the task of the server software to deal with the differences between OSes.

As for your question, just try it for yourself and see what happens. If it works then it works and you don't need us at all. If it doesn't work then you need to look at exactly what did happen, e.g. what exception was thrown and where, and provide that information to us if you need our help to solve the issue.

BTW, if you want the binary data from a file then just call File.ReadAllBytes. It's silly to read the data as text, which requires converting the bytes to text, and then converting the text to bytes.
 
Strange. @patrick was using FluentFtp in his previous thread, which is the right thing to do since the docs recommend not using the built-in FtpWebRequest anymore. In this thread he is back to using FtpWebRequest.
 
Last edited:
BTW, if you want the binary data from a file then just call File.ReadAllBytes. It's silly to read the data as text, which requires converting the bytes to text, and then converting the text to bytes.

Actually, looking more closely at your code, it would be better to create a FileStream and then call CopyTo to copy the data to the request stream. That way, you're not creating a byte array containing all the data needlessly. That would be especially important for large files.
 

Latest posts

Back
Top Bottom