VintaSoft Twain .NET SDK 14.1: Руководство для .NET разработчика
В этом разделе
    Как загрузить отсканированное изображение на FTP сервер?
    В этом разделе
    Вот C#/VB.NET код, который демонстрирует, как загрузить отсканированное изображение в виде JPEG файла на FTP сервер:
    Vintasoft.Twain.ImageUploading.Ftp.FtpUpload _ftpUpload = null;
    
    bool _isDataSending;
    
    
    /// <summary>
    /// Uploads TWAIN acquired image to FTP server.
    /// </summary>
    public void UploadTwainAcquiredImageToFtpServer(Vintasoft.Twain.AcquiredImage acquiredImage)
    {
        try
        {
            _isDataSending = false;
    
            // create FTP uploader
            _ftpUpload = new Vintasoft.Twain.ImageUploading.Ftp.FtpUpload();
    
            // 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)
        {
            System.Console.WriteLine(ex.Message);
        }
    }
    
    /// <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
        System.Console.WriteLine(e.StatusString);
    }
    
    /// <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 (!_isDataSending)
        {
            System.Console.WriteLine(string.Format("Total bytes: {0}", e.BytesTotal));
            _isDataSending = true;
        }
    
        // show current upload progress, in bytes, in the status string
        System.Console.WriteLine(string.Format("Bytes uploaded: {0}", e.BytesUploaded));
    }
    
    /// <summary>
    /// FTP uploading is completed.
    /// </summary>
    private void _ftpUpload_Completed(object sender, Vintasoft.Twain.ImageUploading.Ftp.CompletedEventArgs e)
    {
        if (e.ErrorCode == Vintasoft.Twain.ImageUploading.Ftp.ErrorCode.None)
            System.Console.WriteLine("FTP: Image is uploaded successfully!");
        else
            System.Console.WriteLine(string.Format("FTP error: {0}", e.ErrorString));
    }
    
    Private _ftpUpload As Vintasoft.Twain.ImageUploading.Ftp.FtpUpload = Nothing
    
    Private _isDataSending As Boolean
    
    
    ''' <summary>
    ''' Uploads TWAIN acquired image to FTP server.
    ''' </summary>
    Public Sub UploadTwainAcquiredImageToFtpServer(acquiredImage As Vintasoft.Twain.AcquiredImage)
            Try
                    _isDataSending = False
    
                    ' create FTP uploader
                    _ftpUpload = New Vintasoft.Twain.ImageUploading.Ftp.FtpUpload()
    
                    ' subscribe to the events
                    AddHandler _ftpUpload.StatusChanged, New System.EventHandler(Of Vintasoft.Twain.ImageUploading.Ftp.StatusChangedEventArgs)(AddressOf _ftpUpload_StatusChanged)
                    AddHandler _ftpUpload.ProgressChanged, New System.EventHandler(Of Vintasoft.Twain.ImageUploading.Ftp.ProgressChangedEventArgs)(AddressOf _ftpUpload_ProgressChanged)
                    AddHandler _ftpUpload.Completed, New System.EventHandler(Of Vintasoft.Twain.ImageUploading.Ftp.CompletedEventArgs)(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 System.Exception
                    System.Console.WriteLine(ex.Message)
            End Try
    End Sub
    
    ''' <summary>
    ''' Status of FTP uploading is changed.
    ''' </summary>
    Private Sub _ftpUpload_StatusChanged(sender As Object, e As Vintasoft.Twain.ImageUploading.Ftp.StatusChangedEventArgs)
            ' show current upload status in the status string
            System.Console.WriteLine(e.StatusString)
    End Sub
    
    ''' <summary>
    ''' FTP uploading is in progress.
    ''' </summary>
    Private Sub _ftpUpload_ProgressChanged(sender As Object, e As Vintasoft.Twain.ImageUploading.Ftp.ProgressChangedEventArgs)
            ' show the size of uploading data (once per upload)
            If Not _isDataSending Then
                    System.Console.WriteLine(String.Format("Total bytes: {0}", e.BytesTotal))
                    _isDataSending = True
            End If
    
            ' show current upload progress, in bytes, in the status string
            System.Console.WriteLine(String.Format("Bytes uploaded: {0}", e.BytesUploaded))
    End Sub
    
    ''' <summary>
    ''' FTP uploading is completed.
    ''' </summary>
    Private Sub _ftpUpload_Completed(sender As Object, e As Vintasoft.Twain.ImageUploading.Ftp.CompletedEventArgs)
            If e.ErrorCode = Vintasoft.Twain.ImageUploading.Ftp.ErrorCode.None Then
                    System.Console.WriteLine("FTP: Image is uploaded successfully!")
            Else
                    System.Console.WriteLine(String.Format("FTP error: {0}", e.ErrorString))
            End If
    End Sub