Solved

FtpWebRequest help

Posted on 2007-03-18
3
1,006 Views
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.  

Thanks.
0
Comment
Question by:nespa
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
sara110 earned 500 total points
ID: 18746225
0
 
LVL 1

Author Comment

by:nespa
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.

http://www.ddj.com/dept/database/191601621?pgno=3
http://www.windowsdevcenter.com/pub/a/windows/2006/12/12/building-ftp-services-using-net-20.html?page=last
http://www.thescripts.com/forum/thread278208.html
http://tim.mackey.ie/FtpWebRequestWithProgressBarCFtp.aspx
0
 
LVL 1

Author Comment

by:nespa
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 + "/", "");
                  }
                  else
                  {
                        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)
                              {
                                    try
                                    {
                                          // 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;
                                          }
                                          else
                                          {
                                                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());
                        }
                        else
                        {
                              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);
                  else
                        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:
http://www.codeproject.com/soap/MTOMWebServices.asp?df=100&forumid=250646&exp=0&fr=26&select=1893456

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now