Upload file to FTP using C#

The existing answers are valid, but why re-invent the wheel and bother with lower level WebRequest types while WebClient already implements FTP uploading neatly: using (var client = new WebClient()) { client.Credentials = new NetworkCredential(ftpUsername, ftpPassword); client.UploadFile(“ftp://host/path.zip”, WebRequestMethods.Ftp.UploadFile, localFile); }

How can we show progress bar for upload with FtpWebRequest

The easiest is to use BackgroundWorker and put your code into DoWork event handler. And report progress with BackgroundWorker.ReportProgress. The basic idea: private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { var ftpWebRequest = (FtpWebRequest)WebRequest.Create(“ftp://example.com”); ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile; using (var inputStream = File.OpenRead(fileName)) using (var outputStream = ftpWebRequest.GetRequestStream()) { var buffer = new byte[1024 * 1024]; int … Read more

Upload File to FTP Server on iPhone

I use a PHP Page to post a file to and have PHP handle the uploading…. This code is used to upload a photo, but it can be adapted to work with any file. PHP Code: <?php $uploaddir=”photos/”; $file = basename($_FILES[‘userfile’][‘name’]); $uploadfile = $uploaddir . $file; if (move_uploaded_file($_FILES[‘userfile’][‘tmp_name’], $uploadfile)) { echo “OK”; } else { … Read more

Parsing FtpWebRequest ListDirectoryDetails line

Not sure if you still need this, but this is the solution i came up with: Regex regex = new Regex ( @”^([d-])([rwxt-]{3}){3}\s+\d{1,}\s+.*?(\d{1,})\s+(\w+\s+\d{1,2}\s+(?:\d{4})?)(\d{1,2}:\d{2})?\s+(.+?)\s?$”, RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace ); Match Groups: object type: d : directory – : file Array[3] of permissions (rwx-) File Size Last Modified Date Last Modified Time File/Directory Name

Python FTP get the most recent file by date

For those looking for a full solution for finding the latest file in a folder: MLSD If your FTP server supports MLSD command, a solution is easy: entries = list(ftp.mlsd()) entries.sort(key = lambda entry: entry[1][‘modify’], reverse = True) latest_name = entries[0][0] print(latest_name) LIST If you need to rely on an obsolete LIST command, you have … Read more

Upload files with FTP using PowerShell

I am not sure you can 100% bullet proof the script from not hanging or crashing, as there are things outside your control (what if the server loses power mid-upload?) – but this should provide a solid foundation for getting you started: # create the FtpWebRequest and configure it $ftp = [System.Net.FtpWebRequest]::Create(“ftp://localhost/me.png”) $ftp = [System.Net.FtpWebRequest]$ftp … Read more

C# class to parse WebRequestMethods.Ftp.ListDirectoryDetails FTP response

For the first (DOS/Windows) listing this code will do: FtpWebRequest request = (FtpWebRequest)WebRequest.Create(“ftp://ftp.example.com/”); request.Credentials = new NetworkCredential(“user”, “password”); request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream()); string pattern = @”^(\d+-\d+-\d+\s+\d+:\d+(?:AM|PM))\s+(<DIR>|\d+)\s+(.+)$”; Regex regex = new Regex(pattern); IFormatProvider culture = CultureInfo.GetCultureInfo(“en-us”); while (!reader.EndOfStream) { string line = reader.ReadLine(); Match match = regex.Match(line); string s = match.Groups[1].Value; DateTime … Read more