0 votes
by (320 points)
edited

Is it possible to package using filestream and place the packaged file into a remote server by "copying". or should i place the packaged file by "FTPing" ?

Applies to: File Transfer Pack
by (18.0k points)
edited

Could you be, please, more specific? What do you meen by "FTPing" in contrast to "copying"?

by (320 points)
edited

If I try to place the file using normal "File Copy" option, I call it copying, whereas if I try to place it on a remote FTP location and if I use FTP methodologies to place it, I call it as "FTPing"

1 Answer

0 votes
by (18.0k points)
edited

I'm not sure what do you exactly mean with "packaging a file". But once you have a filestream, you can:

  • either copy the stream to a file on local disc or on network folder using the system CopyTo method,
  • or copy the stream to an FTP server using the Rebex FTP/SSL component.

Here is an example for both ways:

using System;
using System.IO;
using Rebex.Net;

namespace Rebex.Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            bool copyUsingFtp = true;

            // create a source stream 
            // (we obtain it simply by opening a file, it woud be more complicated in your application)
            using (FileStream src = File.OpenRead(@"c:\temp\file1.txt"))
            {
                // copy the stream content to the destination...
                if (copyUsingFtp)
                    // ... etiher using FTP ...
                    FtpFile(src, "/temp/copied_file.txt");
                else 
                    // ... or using system file copy
                    CopyFile(src, @"c:\temp\copied_file.txt");
            }
        }

        private static void CopyFile(FileStream src, string dstFilename)
        {
            // create stream for destination file (in "overwrite" mode)
            using (FileStream dst = new FileStream(dstFilename, FileMode.Create))
            {
                // copy the stream using system CopyTo method (new in .NET 4.0)
                src.CopyTo(dst);
            }
        }

        private static void FtpFile(FileStream src, string dstFilename)
        {
            // create a Rebex FTP/SSL client object
            using (Ftp client = new Ftp())
            {
                // connect and login to the remote FTP server
                client.Connect("ftpserveraddress.com");
                client.Login("username", "password");

                // upload the source stream to the FTP server
                client.PutFile(src, dstFilename);

                // disconnect from the FTP server
                client.Disconnect();
            }
        }
    }
}

Note: The CopyTo method is new in .NET 4.0. For older versions of .NET framework you can copy streams manually e.g according the following article.

...