VintaSoft Twain .NET SDK 14.1: Руководство для .NET разработчика
В этом разделе
    Как загрузить отсканированное изображение на HTTP(S) сервер?
    В этом разделе
    Вот пример, который демонстрирует, как загрузить полученное изображение в виде файла JPEG на HTTP-сервер:
    System.Windows.Forms.Form _parentForm;
    System.Windows.Forms.TextBox _uploadStatus;
    System.Windows.Forms.ProgressBar _progressBar;
    
    Vintasoft.Twain.ImageUploading.Http.HttpUpload _httpUpload = null;
    
    bool _sendingData = false;
    
    
    /// <summary>
    /// Upload acquired image to HTTP server.
    /// </summary>
    /// <param name="acquiredImage"></param>
    public void UploadAcquiredImageAsJpegToHttp(Vintasoft.Twain.AcquiredImage acquiredImage)
    {
        try
        {
            // create HTTP uploader
            _httpUpload = new Vintasoft.Twain.ImageUploading.Http.HttpUpload(_parentForm);
    
            // subscribe to the events
            _httpUpload.StatusChanged += new System.EventHandler<Vintasoft.Twain.ImageUploading.Http.StatusChangedEventArgs>(_httpUpload_StatusChanged);
            _httpUpload.ProgressChanged += new System.EventHandler<Vintasoft.Twain.ImageUploading.Http.ProgressChangedEventArgs>(_httpUpload_ProgressChanged);
            _httpUpload.Completed += new System.EventHandler<Vintasoft.Twain.ImageUploading.Http.CompletedEventArgs>(_httpUpload_Completed);
    
            // set HTTP upload parameters
            _httpUpload.Url = "https://demos.vintasoft.com/AspNetMvcTwainScanningDemo/api/ImageUploadApi";
            _httpUpload.AddTextField("user", "guest");
            _httpUpload.AddTextField("password", "guest");
            _httpUpload.AddFileField("file", "demo.jpg", acquiredImage.GetAsStream(new Vintasoft.Twain.ImageEncoders.TwainJpegEncoderSettings()));
    
            // post data to the server
            _httpUpload.PostData();
        }
        catch (System.Exception ex)
        {
            _uploadStatus.Text = string.Format("{0}{1}", _uploadStatus.Text, ex);
        }
        finally
        {
            // set maximal value of the progress bar
            _progressBar.Maximum = _httpUpload.BytesTotal;
        }
    }
    
    /// <summary>
    /// Status of HTTP uploading is changed.
    /// </summary>
    private void _httpUpload_StatusChanged(object sender, Vintasoft.Twain.ImageUploading.Http.StatusChangedEventArgs e)
    {
        if (_sendingData)
        {
            _uploadStatus.Text = string.Format("{0}{1}", _uploadStatus.Text, System.Environment.NewLine);
            _sendingData = false;
        }
    
        // show current upload status in the status string
        _uploadStatus.Text = string.Format("{0}{1}{2}", _uploadStatus.Text, e.StatusString, System.Environment.NewLine);
    }
    
    /// <summary>
    /// HTTP uploading is in progress.
    /// </summary>
    private void _httpUpload_ProgressChanged(object sender, Vintasoft.Twain.ImageUploading.Http.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>
    /// HTTP uploading is completed.
    /// </summary>
    private void _httpUpload_Completed(object sender, Vintasoft.Twain.ImageUploading.Http.CompletedEventArgs e)
    {
        _uploadStatus.Text = string.Format("{0}{1}", _uploadStatus.Text, System.Environment.NewLine);
    
        // if no errors
        if (e.ErrorCode == 0)
        {
            // if server returned "OK" status
            if (e.ResponseCode == System.Net.HttpStatusCode.OK)
            {
                _uploadStatus.Text = string.Format("{0}HTTP: Image is uploaded successfully!{1}", _uploadStatus.Text, System.Environment.NewLine);
                _uploadStatus.Text = string.Format("{0}{1}", _uploadStatus.Text, System.Environment.NewLine);
                _uploadStatus.Text = string.Format("{0}Response content: {1}", _uploadStatus.Text, e.ResponseContent);
            }
            // if server returned NOT "OK" status
            else
            {
                _uploadStatus.Text = string.Format("{0}Response code: {1}{2}", _uploadStatus.Text, e.ResponseCode, System.Environment.NewLine);
                _uploadStatus.Text = string.Format("{0}{1}", System.Environment.NewLine);
                _uploadStatus.Text = string.Format("{0}Response string: {1}", _uploadStatus.Text, e.ResponseString);
            }
        }
        // if error occurs
        else
            _uploadStatus.Text = string.Format("{0}Error: {1}", _uploadStatus.Text, e.ErrorString);
    
        _uploadStatus.Text = string.Format("{0}{1}", _uploadStatus.Text, System.Environment.NewLine);
    }
    
    Dim _parentForm As Form
    Dim _uploadStatus As TextBox
    Dim _progressBar As ProgressBar
    
    Dim _httpUpload As Vintasoft.Twain.ImageUploading.Http.HttpUpload
    
    Dim _sendingData As Boolean = False
    
    
    ' Upload acquired image to HTTP server.
    Private Sub UploadAcquiredImageAsJpegToHttp(ByVal acquiredImage As Vintasoft.Twain.AcquiredImage)
        Try
            ' create HTTP uploader
            _httpUpload = New Vintasoft.Twain.ImageUploading.Http.HttpUpload(_parentForm)
    
            ' subscribe to the events
            AddHandler _httpUpload.StatusChanged, AddressOf _httpUpload_StatusChanged
            AddHandler _httpUpload.ProgressChanged, AddressOf _httpUpload_ProgressChanged
            AddHandler _httpUpload.Completed, AddressOf _httpUpload_Completed
    
            ' set HTTP upload parameters
            _httpUpload.Url = "https://demos.vintasoft.com/AspNetMvcTwainScanningDemo/api/ImageUploadApi"
            _httpUpload.AddTextField("user", "guest")
            _httpUpload.AddTextField("password", "guest")
            _httpUpload.AddFileField("file", "demo.jpg", acquiredImage.GetAsStream(New Vintasoft.Twain.ImageEncoders.TwainJpegEncoderSettings()))
    
            ' post data to the server
            _httpUpload.PostData()
        Catch ex As Exception
            _uploadStatus.Text = String.Format("{0}{1}", _uploadStatus.Text, ex)
        Finally
            ' set maximal value of the progress bar
            _progressBar.Maximum = _httpUpload.BytesTotal
        End Try
    End Sub
    
    ' Handler of the upload status changed event.
    Private Sub _httpUpload_StatusChanged(ByVal sender As Object, ByVal e As Vintasoft.Twain.ImageUploading.Http.StatusChangedEventArgs)
        If _sendingData Then
            _uploadStatus.Text = String.Format("{0}{1}", _uploadStatus.Text, Environment.NewLine)
            _sendingData = False
        End If
    
        ' 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 _httpUpload_ProgressChanged(ByVal sender As Object, ByVal e As Vintasoft.Twain.ImageUploading.Http.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 _httpUpload_Completed(ByVal sender As Object, ByVal e As Vintasoft.Twain.ImageUploading.Http.CompletedEventArgs)
        _uploadStatus.Text = String.Format("{0}{1}", _uploadStatus.Text, Environment.NewLine)
    
        If e.ErrorCode = 0 Then  ' if no errors
            If e.ResponseCode = Net.HttpStatusCode.OK Then  ' if server returned "OK" status
                _uploadStatus.Text = String.Format("{0}HTTP: Image is uploaded successfully!{1}", _uploadStatus.Text, Environment.NewLine)
                _uploadStatus.Text = String.Format("{0}{1}", _uploadStatus.Text, Environment.NewLine)
                _uploadStatus.Text = String.Format("{0}Response content: {1}", _uploadStatus.Text, e.ResponseContent)
    
            Else  ' if server returned NOT "OK" status
                _uploadStatus.Text = String.Format("{0}Response code: {1}{2}", _uploadStatus.Text, e.ResponseCode, Environment.NewLine)
                _uploadStatus.Text = String.Format("{0}", Environment.NewLine)
                _uploadStatus.Text = String.Format("{0}Response string: {1}", _uploadStatus.Text, e.ResponseString)
            End If
    
    
        Else  ' if error occurs
            _uploadStatus.Text = String.Format("{0}Error: {1}", _uploadStatus.Text, e.ErrorString)
        End If
    
        _uploadStatus.Text = String.Format("{0}{1}", _uploadStatus.Text, Environment.NewLine)
    
        RaiseEvent Completed(Me, EventArgs.Empty)
    End Sub