2

Closed

Stream based upload

description

Add stream based upload when uploading from memory or other non-file source. Suggestion from LiqdFire via CodeProject
Closed Jul 21, 2008 at 1:35 PM by Quango

comments

Quango wrote Apr 25, 2007 at 9:38 PM

/// <summary>        /// Upload a local file to the FTP server        /// </summary>        /// <param name="fi">Source file</param>        /// <param name="targetFilename">Target filename (optional)</param>        /// <returns></returns>        public bool Upload(FileInfo fi, string targetFilename)        {          //copy the file specified to target file: target file can be full path or just filename (uses current dir)          //1. check target          string target;          if (targetFilename.Trim() == "")          {              //Blank target: use source filename & current dir              target = this.CurrentDirectory + fi.Name;          }          else if (targetFilename.Contains("/"))          {              //If contains / treat as a full path              target = AdjustDir(targetFilename);          }          else          {              //otherwise treat as filename only, use current directory              target = CurrentDirectory + targetFilename;          }          using (FileStream fs = fi.OpenRead())          {            try            {              return Upload(fs, target);            }            catch (Exception ex)            {              System.Diagnostics.Trace.WriteLine(ex.ToString());            }            finally            {              //ensure file closed              fs.Close();            }          }          return false;        }        /// <summary>        /// Upload a local file to the FTP server        /// </summary>        /// <param name="SS">Source Stream</param>        /// <param name="targetFilename">Target filename (optional)</param>        /// <returns></returns>        public bool Upload(Stream SS, string targetFilename)         {          string URI = Hostname + targetFilename;          //perform copy          System.Net.FtpWebRequest ftp = GetRequest(URI);          //Set request to upload a file in binary          ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile;          ftp.UseBinary = true;          //Notify FTP of the expected size          ftp.ContentLength = SS.Length;          //create byte array to store: ensure at least 1 byte!          const int BufferSize = 2048;          byte[] content = new byte[BufferSize - 1 + 1];          int dataRead;          //open file for reading          using (SS)          {            try            {              SS.Position = 0;              //open request to send              using (Stream rs = ftp.GetRequestStream())              {                do                {                  dataRead = SS.Read(content, 0, BufferSize);                  rs.Write(content, 0, dataRead);                } while (!(dataRead < BufferSize));                rs.Close();              }              return true;            }            catch (Exception)            {            }            finally            {              //ensure file closed              SS.Close();              ftp = null;            }          }          return false;        }</code>

Quango wrote Apr 25, 2007 at 9:38 PM

I have modified this function slightly as it does not work if CurrentDirectory is being used and the user calls Upload(stream, 'myfile.bin') ie. uses relative paths.

wrote Jun 2, 2007 at 6:56 PM

wrote Jun 13, 2007 at 7:15 AM

wrote Nov 21, 2007 at 8:24 AM

Stream based uploads added to FtpClient.

wrote Nov 21, 2007 at 8:29 AM

wrote Jul 21, 2008 at 1:35 PM

Resolved with changeset 15142.

wrote Feb 14, 2013 at 6:48 PM

wrote May 16, 2013 at 9:08 AM