VintaSoft Twain .NET SDK v10.2
Vintasoft.Twain.ImageUploading.Ftp Namespace / FtpUpload Class / PostData Method
Example




In This Topic
    PostData Method (FtpUpload)
    In This Topic
    Sends data to the FTP server.
    Syntax
    'Declaration
     
    Public Function PostData() As Boolean
    'Usage
     
    Dim instance As FtpUpload
    Dim value As Boolean
     
    value = instance.PostData()
    public bool PostData()
    public: bool PostData(); 
    public:
    bool PostData(); 

    Return Value

    true if upload process was started successfully, false otherwise.
    Exceptions
    ExceptionDescription
    Thrown if host is null.
    Thrown if there is no files to transfer.
    Remarks
    This method initializes asynchronous file uploading process and file will be uploaded to the server only when the Completed event will be generated or value of the StatusCode property will be set to StatusCode.Completed.

    An uploading process can be aborted with the Abort method.
    Example
    This example shows how to upload acquired image as JPEG file to FTP server.
    Dim _parentForm As Form
    Dim _uploadStatus As TextBox
    Dim _progressBar As ProgressBar
                
    Dim _ftpUpload As FtpUpload
                
    Dim _sendingData As Boolean = False
                
                
    ' Upload acquired image to HTTP server.
    Private Sub UploadAcquiredImageAsJpegToFtp(ByVal acquiredImage As AcquiredImage)
        Try
            ' create FTP uploader
            _ftpUpload = New FtpUpload(_parentForm)
                
            ' subscribe to the events
            AddHandler _ftpUpload.StatusChanged, AddressOf _ftpUpload_StatusChanged
            AddHandler _ftpUpload.ProgressChanged, AddressOf _ftpUpload_ProgressChanged
            AddHandler _ftpUpload.Completed, AddressOf _ftpUpload_Completed
                
            ' set FTP upload parameters
            _ftpUpload.Host = "ftp.test.com"
            _ftpUpload.User = "user"
            _ftpUpload.Password = "password"
            _ftpUpload.PassiveMode = True
            _ftpUpload.Timeout = 2000
            _ftpUpload.Path = "/images/"
            _ftpUpload.AddFile("scan.jpg", acquiredImage.GetAsStream(New TwainJpegEncoderSettings()))
                
            ' post data to the server
            _ftpUpload.PostData()
                
        Catch ex As Exception
            _uploadStatus.Text = String.Format("{0}{1}", _uploadStatus.Text, ex)
        Finally
            ' set maximal value of the progress bar
            _progressBar.Maximum = _ftpUpload.BytesTotal
        End Try
    End Sub
                
    ' Handler of the upload status changed event.
    Private Sub _ftpUpload_StatusChanged(ByVal sender As System.Object, _
                                         ByVal e As Vintasoft.Twain.ImageUploading.Ftp.StatusChangedEventArgs)
        ' show current upload status in the status string
        _uploadStatus.Text = String.Format("{0}{1}{2}", _uploadStatus.Text, e.StatusString, Environment.NewLine)
    End Sub
                
    ' Handler of the upload progress event.
    Private Sub _ftpUpload_ProgressChanged(ByVal sender As Object, _
                                            ByVal e As Vintasoft.Twain.ImageUploading.Ftp.ProgressChangedEventArgs)
        ' show the size of uploading data (once per upload)
        If Not _sendingData Then
            _uploadStatus.Text = String.Format("{0}({1} bytes){2}", _uploadStatus.Text, e.BytesTotal, Environment.NewLine)
            _sendingData = True
        End If
                
        ' show current upload progress in progress bar
        _progressBar.Value = e.BytesUploaded
                
        ' show current upload progress, in bytes, in the status string
        _uploadStatus.Text = String.Format("{0}{1} ", _uploadStatus.Text, e.BytesUploaded)
    End Sub
                
    ' Handler of the upload completed event.
    Private Sub _ftpUpload_Completed(ByVal sender As Object, _
                                     ByVal e As Vintasoft.Twain.ImageUploading.Ftp.CompletedEventArgs)
        _uploadStatus.Text = String.Format("{0}{1}", _uploadStatus.Text, Environment.NewLine)
                
        If e.ErrorCode = Vintasoft.Twain.ImageUploading.Ftp.ErrorCode.None Then
            _uploadStatus.Text = String.Format("{0}FTP: Image is uploaded successfully!", _uploadStatus.Text)
        Else
            _uploadStatus.Text = String.Format("{0}FTP error: {1}", _uploadStatus.Text, e.ErrorString)
        End If
                
        _uploadStatus.Text = String.Format("{0}", Environment.NewLine)
    End Sub
    Form _parentForm;
    TextBox _uploadStatus;
    ProgressBar _progressBar;
                
    FtpUpload _ftpUpload = null;
                
    bool _sendingData;
                
                
    /// <summary>
    /// Upload acquired image to FTP server.
    /// </summary>
    public void UploadAcquiredImageAsJpegToFtp(AcquiredImage acquiredImage)
    {
        try
        {
            // create FTP uploader
            _ftpUpload = new FtpUpload(_parentForm);
                
            // subscribe to the events
            _ftpUpload.StatusChanged += new EventHandler<StatusChangedEventArgs>(_ftpUpload_StatusChanged);
            _ftpUpload.ProgressChanged += new EventHandler<ProgressChangedEventArgs>(_ftpUpload_ProgressChanged);
            _ftpUpload.Completed += new EventHandler<CompletedEventArgs>(_ftpUpload_Completed);
                
            // set FTP upload parameters
            _ftpUpload.Host = "ftp.test.com";
            _ftpUpload.User = "user";
            _ftpUpload.Password = "password";
            _ftpUpload.PassiveMode = true;
            _ftpUpload.Timeout = 2000;
            _ftpUpload.Path = "/images/";
            _ftpUpload.AddFile("scan.jpg", acquiredImage.GetAsStream(new TwainJpegEncoderSettings()));
                
            // post data to the server
            _ftpUpload.PostData();
        }
        catch (Exception ex)
        {
            _uploadStatus.Text = string.Format("{0}{1}", _uploadStatus.Text, ex);
        }
        finally
        {
            // set maximal value of the progress bar
            _progressBar.Maximum = _ftpUpload.BytesTotal;
        }
    }
                
    /// <summary>
    /// Status of FTP uploading is changed.
    /// </summary>
    private void _ftpUpload_StatusChanged(object sender, StatusChangedEventArgs e)
    {
        // show current upload status in the status string
        _uploadStatus.Text = string.Format("{0}{1}{2}", _uploadStatus.Text, e.StatusString, Environment.NewLine);
    }
                
    /// <summary>
    /// FTP uploading is in progress.
    /// </summary>
    private void _ftpUpload_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // show the size of uploading data (once per upload)
        if (!_sendingData)
        {
            _uploadStatus.Text = string.Format("{0}({1} bytes){2}", _uploadStatus.Text, e.BytesTotal, Environment.NewLine);
            _sendingData = true;
        }
                
        // show current upload progress in progress bar
        _progressBar.Value = e.BytesUploaded;
                
        // show current upload progress, in bytes, in the status string
        _uploadStatus.Text = string.Format("{0}{1} ", _uploadStatus.Text, e.BytesUploaded);
    }
                
    /// <summary>
    /// FTP uploading is completed.
    /// </summary>
    private void _ftpUpload_Completed(object sender, Vintasoft.Twain.ImageUploading.Ftp.CompletedEventArgs e)
    {
        _uploadStatus.Text = string.Format("{0}{1}", _uploadStatus.Text, Environment.NewLine);
                
        if (e.ErrorCode == Vintasoft.Twain.ImageUploading.Ftp.ErrorCode.None)
            _uploadStatus.Text = string.Format("{0}FTP: Image is uploaded successfully!", _uploadStatus.Text);
        else
            _uploadStatus.Text = string.Format("{0}FTP error: {1}", _uploadStatus.Text, e.ErrorString);
                
        _uploadStatus.Text = string.Format("{0}", Environment.NewLine);
    }
    Requirements

    Target Platforms: Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also