FtpWebRequest help

Posted on 2007-03-18
Medium Priority
Last Modified: 2007-11-27
Any success w/ FtpWebRequest in 2.0?   I've been working w/ a few examples & can't seem to get any to work.  A step in the right direction would be a huge help.  

I don't need anything complex (i.e., I can make it asynchronous request later on)... some simple code & a URI example would be most helpful.  

Question by:nespa
  • 2

Accepted Solution

sara110 earned 2000 total points
ID: 18746225

Author Comment

ID: 18747821
Yep - saw those; those were some of the first tests I used to try to get it to work as they're the top of the Google results list for FtpWebRequest.      Did you try any of those / did they work for you?  They didn't on my end - that's why I was asking; to see if anyone had conclusive results using specific code.

I wonder if it's a URI issue / how I'm formatting - I'll keep testing & that's why I was also asking for URI examples to make sure I'm on target.  I understand escaping (\) or using @ for the string is important too...but I'm doing that.  

The reason this stuff is tricky is (obviously) it's a combination of getting the FTP settings on the server side, settings for firewalls on the client & server & specific code which make a successful file upload.  

I'm looking for specific sample code that worked for someone so that I can work backwards & determine which of the above settings I need to change on my end to make it work.

Here are others I tested:  the first was great - good explanations.


Author Comment

ID: 18769017
Forgot to close this question out -- what worked for me was a post w/ some code I modified.   Here's the code that worked... and I think most of my problems might have been due to incorrect testing data... stuff like forgetting to escape the backslash while testing; simple stupid stuff.   Anyway, here's what worked:
            public string UploadFile(string ftpHost, string targetFolderOnHost, string filename, string ftpUsername, string ftpPword, bool returnUniqueNewFilename)
                  System.Collections.ArrayList errors = new System.Collections.ArrayList();
                  string requestMethod = "";
                  string uploadPath = "";

                  // set up the host string to request.  this includes the target folder and the target file name (based on the source filename)
                  if (returnUniqueNewFilename)
                        requestMethod = System.Net.WebRequestMethods.Ftp.UploadFileWithUniqueName;
                        uploadPath = String.Format("{0}/{1}{2}", ftpHost, targetFolderOnHost == "" ? "" : targetFolderOnHost + "/", "");
                        requestMethod = System.Net.WebRequestMethods.Ftp.UploadFile;
                        uploadPath = String.Format("{0}/{1}{2}", ftpHost, targetFolderOnHost == "" ? "" : targetFolderOnHost + "/", Path.GetFileName(filename));

                  if (!uploadPath.ToLower().StartsWith("ftp://"))
                        uploadPath = "ftp://" + uploadPath;

                  System.Net.FtpWebRequest request = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(uploadPath);
                  request.UseBinary = true;
                  request.UsePassive = true;
                  request.Method = requestMethod;
                  request.Credentials = new System.Net.NetworkCredential(ftpUsername, ftpPword);

                  // Copy the contents of the file to the request stream.
                  long FileSize = new System.IO.FileInfo(filename).Length;
                  string FileSizeDescription = GetFileSize(FileSize); // e.g. "2.4 Gb" instead of 240000000000000 bytes etc...                  
                  int ChunkSize = 4096, NumRetries = 0, MaxRetries = 50;
                  long SentBytes = 0;
                  byte[] Buffer = new byte[ChunkSize];    // this buffer stores each chunk, for sending to the web service via MTOM
                  using (System.IO.Stream requestStream = request.GetRequestStream())
                        using (System.IO.FileStream fs = System.IO.File.Open(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
                              int BytesRead = fs.Read(Buffer, 0, ChunkSize);      // read the first chunk in the buffer
                              // send the chunks to the web service one by one, until FileStream.Read() returns 0, meaning the entire file has been read.
                              while (BytesRead > 0)
                                          // send this chunk to the server.  it is sent as a byte[] parameter, but the client and server have been configured to encode byte[] using MTOM.
                                          requestStream.Write(Buffer, 0, BytesRead);

                                          // sentBytes is only updated AFTER a successful send of the bytes. so it would be possible to build in 'retry' code, to resume the upload from the current SentBytes position if AppendChunk fails.
                                          SentBytes += BytesRead;

                                          // update the user interface
                                          string SummaryText = String.Format("Transferred {0} / {1}", GetFileSize(SentBytes), FileSizeDescription);
                                    catch (Exception ex)
                                          errors.Add("Exception: " + ex.ToString());
                                          if (NumRetries++ < MaxRetries)
                                                // rewind the filestream and keep trying
                                                fs.Position -= BytesRead;
                                                throw new Exception(String.Format("Error occurred during upload, too many retries. \n{0}", ex.ToString()));
                                    BytesRead = fs.Read(Buffer, 0, ChunkSize);      // read the next chunk (if it exists) into the buffer.  the while loop will terminate if there is nothing left to read
                  using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                        if (errors.Count > 0)
                              System.Text.StringBuilder sb = new System.Text.StringBuilder();
                              foreach (string str in errors)
                                    sb.Append(str + System.Environment.NewLine);    //windows
                                    //sb.Append(str + "<br />");                                    //web
                              throw new Exception("File upload completed, however there were errors: " + sb.ToString());
                              return String.Format("Upload File Complete, status {0}", response.StatusDescription);

            /// <summary>
            /// Returns a description of a number of bytes, in appropriate units.
            /// e.g.
            ///            passing in 1024 will return a string "1 Kb"
            ///            passing in 1230000 will return "1.23 Mb"
            /// Megabytes and Gigabytes are formatted to 2 decimal places.
            /// Kilobytes are rounded to whole numbers.
            /// If the rounding results in 0 Kb, "1 Kb" is returned, because Windows behaves like this also.
            /// </summary>
            public static string GetFileSize(long numBytes)
                  string fileSize = "";

                  if (numBytes > 1073741824)
                        fileSize = String.Format("{0:0.00} Gb", (double)numBytes / 1073741824);
                  else if (numBytes > 1048576)
                        fileSize = String.Format("{0:0.00} Mb", (double)numBytes / 1048576);
                        fileSize = String.Format("{0:0} Kb", (double)numBytes / 1024);

                  if (fileSize == "0 Kb")
                        fileSize = "1 Kb";      // min.                                          
                  return fileSize;

my code above is based on the following:


Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…
Did you know PowerShell can save you time with SaaS platforms? Simply leverage RESTfulAPIs to build your own PowerShell modules. These will kill repetitive tickets and tabs, using the command Invoke-RestMethod. Tune into this webinar to learn how…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question