VintaSoft Twain .NET SDK 14.1: Руководство для .NET разработчика
В этом разделе
    Как загрузить отсканированное изображение на HTTP(S) сервер?
    В этом разделе
    Вот C#/VB.NET код, который демонстрирует, как загрузить отсканированное изображение в виде JPEG файла на HTTP сервер:

    Vintasoft.Twain.ImageUploading.Http.HttpUpload _httpUpload = null;
    
    bool _isDataSending = 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();
    
            // 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/AspNetCoreTwainScanningDemo/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)
        {
            System.Console.WriteLine(ex.Message);
        }
    }
    
    /// <summary>
    /// Status of HTTP uploading is changed.
    /// </summary>
    private void _httpUpload_StatusChanged(object sender, Vintasoft.Twain.ImageUploading.Http.StatusChangedEventArgs e)
    {
        System.Console.WriteLine(e.StatusString);
    }
    
    /// <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 (!_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>
    /// HTTP uploading is completed.
    /// </summary>
    private void _httpUpload_Completed(object sender, Vintasoft.Twain.ImageUploading.Http.CompletedEventArgs e)
    {
        // if no errors
        if (e.ErrorCode == 0)
        {
            // if server returned "OK" status
            if (e.ResponseCode == System.Net.HttpStatusCode.OK)
            {
                System.Console.WriteLine(string.Format("HTTP: Image is uploaded successfully!"));
                System.Console.WriteLine(string.Format("Response content: {0}", e.ResponseContent));
            }
            // if server returned NOT "OK" status
            else
            {
                System.Console.WriteLine(string.Format("Response code: {0}", e.ResponseCode));
                System.Console.WriteLine(string.Format("Response string: {0}", e.ResponseString));
            }
        }
        // if error occurs
        else
            System.Console.WriteLine(string.Format("Error: {0}", e.ErrorString));
    }
    
    Private _httpUpload As Vintasoft.Twain.ImageUploading.Http.HttpUpload = Nothing
    
    Private _isDataSending As Boolean = False
    
    
    ''' <summary>
    ''' Upload acquired image to HTTP server.
    ''' </summary>
    ''' <param name="acquiredImage"></param>
    Public Sub UploadAcquiredImageAsJpegToHttp(acquiredImage As Vintasoft.Twain.AcquiredImage)
            Try
                    ' create HTTP uploader
                    _httpUpload = New Vintasoft.Twain.ImageUploading.Http.HttpUpload()
    
                    ' subscribe to the events
                    AddHandler _httpUpload.StatusChanged, New System.EventHandler(Of Vintasoft.Twain.ImageUploading.Http.StatusChangedEventArgs)(AddressOf _httpUpload_StatusChanged)
                    AddHandler _httpUpload.ProgressChanged, New System.EventHandler(Of Vintasoft.Twain.ImageUploading.Http.ProgressChangedEventArgs)(AddressOf _httpUpload_ProgressChanged)
                    AddHandler _httpUpload.Completed, New System.EventHandler(Of Vintasoft.Twain.ImageUploading.Http.CompletedEventArgs)(AddressOf _httpUpload_Completed)
    
                    ' set HTTP upload parameters
                    _httpUpload.Url = "https://demos.vintasoft.com/AspNetCoreTwainScanningDemo/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 System.Exception
                    System.Console.WriteLine(ex.Message)
            End Try
    End Sub
    
    ''' <summary>
    ''' Status of HTTP uploading is changed.
    ''' </summary>
    Private Sub _httpUpload_StatusChanged(sender As Object, e As Vintasoft.Twain.ImageUploading.Http.StatusChangedEventArgs)
            System.Console.WriteLine(e.StatusString)
    End Sub
    
    ''' <summary>
    ''' HTTP uploading is in progress.
    ''' </summary>
    Private Sub _httpUpload_ProgressChanged(sender As Object, e As Vintasoft.Twain.ImageUploading.Http.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>
    ''' HTTP uploading is completed.
    ''' </summary>
    Private Sub _httpUpload_Completed(sender As Object, e As Vintasoft.Twain.ImageUploading.Http.CompletedEventArgs)
            ' if no errors
            If e.ErrorCode = 0 Then
                    ' if server returned "OK" status
                    If e.ResponseCode = System.Net.HttpStatusCode.OK Then
                            System.Console.WriteLine(String.Format("HTTP: Image is uploaded successfully!"))
                            System.Console.WriteLine(String.Format("Response content: {0}", e.ResponseContent))
                    Else
                            ' if server returned NOT "OK" status
                            System.Console.WriteLine(String.Format("Response code: {0}", e.ResponseCode))
                            System.Console.WriteLine(String.Format("Response string: {0}", e.ResponseString))
                    End If
            Else
                    ' if error occurs
                    System.Console.WriteLine(String.Format("Error: {0}", e.ErrorString))
            End If
    End Sub