VintaSoft Twain .NET SDK 14.1: Руководство для .NET разработчика
В этом разделе
    Как загрузить отсканированное изображение на FTP-сервер?
    В этом разделе
    Вот пример, который демонстрирует, как загрузить полученное изображение в формате JPEG на FTP-сервер:
    System.Windows.Forms.Form _parentForm;
    System.Windows.Forms.TextBox _uploadStatus;
    System.Windows.Forms.ProgressBar _progressBar;
    
    Vintasoft.Twain.ImageUploading.Ftp.FtpUpload _ftpUpload = null;
    
    bool _sendingData;
    
    
    /// <summary>
    /// Upload acquired image to FTP server.
    /// </summary>
    public void UploadAcquiredImageAsJpegToFtp(Vintasoft.Twain.AcquiredImage acquiredImage)
    {
        try
        {
            // create FTP uploader
            _ftpUpload = new Vintasoft.Twain.ImageUploading.Ftp.FtpUpload(_parentForm);
    
            // subscribe to the events
            _ftpUpload.StatusChanged += new System.EventHandler<Vintasoft.Twain.ImageUploading.Ftp.StatusChangedEventArgs>(_ftpUpload_StatusChanged);
            _ftpUpload.ProgressChanged += new System.EventHandler<Vintasoft.Twain.ImageUploading.Ftp.ProgressChangedEventArgs>(_ftpUpload_ProgressChanged);
            _ftpUpload.Completed += new System.EventHandler<Vintasoft.Twain.ImageUploading.Ftp.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 Vintasoft.Twain.ImageEncoders.TwainJpegEncoderSettings()));
    
            // post data to the server
            _ftpUpload.PostData();
        }
        catch (System.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, Vintasoft.Twain.ImageUploading.Ftp.StatusChangedEventArgs e)
    {
        // show current upload status in the status string
        _uploadStatus.Text = string.Format("{0}{1}{2}", _uploadStatus.Text, e.StatusString, System.Environment.NewLine);
    }
    
    /// <summary>
    /// FTP uploading is in progress.
    /// </summary>
    private void _ftpUpload_ProgressChanged(object sender, Vintasoft.Twain.ImageUploading.Ftp.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, System.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, System.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}", System.Environment.NewLine);
    }
    
    Dim _parentForm As Form
    Dim _uploadStatus As TextBox
    Dim _progressBar As ProgressBar
    
    Dim _ftpUpload As Vintasoft.Twain.ImageUploading.Ftp.FtpUpload
    
    Dim _sendingData As Boolean = False
    
    
    ' Upload acquired image to HTTP server.
    Private Sub UploadAcquiredImageAsJpegToFtp(ByVal acquiredImage As Vintasoft.Twain.AcquiredImage)
        Try
            ' create FTP uploader
            _ftpUpload = New Vintasoft.Twain.ImageUploading.Ftp.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 Vintasoft.Twain.ImageEncoders.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