C++/CLI Code Snippet - Upload file to FTP Server

C++/CLI Code Snippet - Upload file to FTP Server

C++/CLI Code Snippet - Upload file to FTP Server

C++/CLI code snippet upload file to FTP Server. This method login to FTP server using username and password and upload the local file to given FTP location.

Bookmark:

C++/CLI Code Snippet - Upload file to FTP Server

This .Net C++/CLI code snippet upload file to FTP Server. To use this function simply provide the local file name to upload, upload path including host name, FTP username and FTP password. This function uses System.Net and System.IO namespaces to upload the file.

To upload file we create two streams, one for the FTP connection and another for the file we are reading from local disk to upload. These are the steps we take to upload a file: Create a FtpWebRequest object, set the FtpWebRequest.Method property to UploadFile, set the FtpWebRequest.Credentials property to our login information, get the request stream of the FtpWebRequest object. This is the stream we will write to, open the file we are going to upload and get a stream to its data, read the file data from the input stream and write it into the request stream and close the uploaded file stream and the request stream.

/// <summary>
/// Methods to upload file to FTP Server
/// </summary>
/// <param name="_FileName">local source file name</param>
/// <param name="_UploadPath">Upload FTP path including Host name</param>
/// <param name="_FTPUser">FTP login username</param>
/// <param name="_FTPPass">FTP login password</param>
void UploadFile(System::String ^_FileName, System::String ^_UploadPath, System::String ^_FTPUser, System::String ^_FTPPass)
{
    System::IO::FileInfo ^_FileInfo = gcnew System::IO::FileInfo(_FileName);

    // Create FtpWebRequest object from the Uri provided
    System::Net::FtpWebRequest ^_FtpWebRequest = safe_cast<System::Net::FtpWebRequest^>(System::Net::FtpWebRequest::Create(gcnew Uri(_UploadPath)));

    // Provide the WebPermission Credintials
    _FtpWebRequest->Credentials = gcnew System::Net::NetworkCredential(_FTPUser, _FTPPass);

    // By default KeepAlive is true, where the control connection is not closed
    // after a command is executed.
    _FtpWebRequest->KeepAlive = false;

    // set timeout for 20 seconds
    _FtpWebRequest->Timeout = 20000;

    // Specify the command to be executed.
    _FtpWebRequest->Method = System::Net::WebRequestMethods::Ftp.UploadFile;

    // Specify the data transfer type.
    _FtpWebRequest->UseBinary = true;

    // Notify the server about the size of the uploaded file
    _FtpWebRequest->ContentLength = _FileInfo->Length;

    // The buffer size is set to 2kb
    int buffLength = 2048;
    array<System::Byte> ^buff = gcnew array<System::Byte>(buffLength);

    // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
    System::IO::FileStream ^_FileStream = _FileInfo->OpenRead();

    try
    {
        // Stream to which the file to be upload is written
        System::IO::Stream ^_Stream = _FtpWebRequest->GetRequestStream();

        // Read from the file stream 2kb at a time
        int contentLen = _FileStream->Read(buff, 0, buffLength);

        // Till Stream content ends
        while (contentLen != 0)
        {
            // Write Content from the file stream to the FTP Upload Stream
            _Stream->Write(buff, 0, contentLen);
            contentLen = _FileStream->Read(buff, 0, buffLength);
        }

        // Close the file stream and the Request Stream
        _Stream->Close();
        delete _Stream;
        _FileStream->Close();
        delete _FileStream;
    }
    catch (Exception ^ex)
    {
        MessageBox::Show(ex->Message, "Upload Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
    }
}


Here is a simple example showing how to use above method (UploadFile) to upload file to FTP Server.

// Upload file using FTP
UploadFile("c:\\UploadFile.doc", "ftp://FTPHostName/UploadPath/UploadFile.doc", "UserName", "Password");


C++/CLI Keywords Used:

  • FileInfo
  • FtpWebRequest
  • Credentials
  • KeepAlive
  • Timeout
  • UploadFile
  • UseBinary
  • ContentLength
  • FileStream
  • Exception

Code Snippet Information:

  • Applies To: Visual Studio, .Net, C++, CLI, Upload File, FTP Server, FTP Upload, FTP, FTP Web Request, File Stream
  • Programming Language : C++/CLI

External Resources:

Leave a comment