VintaSoft Twain .NET SDK v10.2
PostData Method (HttpUpload)
Example 




Vintasoft.Twain.ImageUploading.Http Namespace > HttpUpload Class : PostData Method
Sends data to the HTTP/HTTPS server by the POST method.
Syntax
'Declaration
 
Public Function PostData() As Boolean
'Usage
 
Dim instance As HttpUpload
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 URL is null.
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 HTTP server.
Dim _parentForm As Form
Dim _uploadStatus As TextBox
Dim _progressBar As ProgressBar
            
Dim _httpUpload As HttpUpload
            
Dim _sendingData As Boolean = False
            
            
' Upload acquired image to HTTP server.
Private Sub UploadAcquiredImageAsJpegToHttp(ByVal acquiredImage As AcquiredImage)
    Try
        ' create HTTP uploader
        _httpUpload = New 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 = "http://demos.vintasoft.com/WebTwainDemo/ImageUpload.aspx"
        _httpUpload.AddTextField("user", "guest")
        _httpUpload.AddTextField("password", "guest")
        _httpUpload.AddFileField("file", "demo.jpg", acquiredImage.GetAsStream(New 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}{1}", 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
Form _parentForm;
TextBox _uploadStatus;
ProgressBar _progressBar;
            
HttpUpload _httpUpload = null;
            
bool _sendingData = false;
            
            
// Upload acquired image to HTTP server.
// <param name="acquiredImage"></param>
public void UploadAcquiredImageAsJpegToHttp(AcquiredImage acquiredImage)
{
    try
    {
        // create HTTP uploader
        _httpUpload = new HttpUpload(_parentForm);
            
        // subscribe to the events
        _httpUpload.StatusChanged += new EventHandler<StatusChangedEventArgs>(_httpUpload_StatusChanged);
        _httpUpload.ProgressChanged += new EventHandler<ProgressChangedEventArgs>(_httpUpload_ProgressChanged);
        _httpUpload.Completed += new EventHandler<CompletedEventArgs>(_httpUpload_Completed);
            
        // set HTTP upload parameters
        _httpUpload.Url = "http://demos.vintasoft.com/WebTwainDemo/ImageUpload.aspx";
        _httpUpload.AddTextField("user", "guest");
        _httpUpload.AddTextField("password", "guest");
        _httpUpload.AddFileField("file", "demo.jpg", acquiredImage.GetAsStream(new TwainJpegEncoderSettings()));
            
        // post data to the server
        _httpUpload.PostData();
    }
    catch (Exception ex)
    {
        _uploadStatus.Text = string.Format("{0}{1}", _uploadStatus.Text, ex);
    }
    finally
    {
        // set maximal value of the progress bar
        _progressBar.Maximum = _httpUpload.BytesTotal;
    }
}
            
// Status of HTTP uploading is changed.
private void _httpUpload_StatusChanged(object sender, Vintasoft.Twain.ImageUploading.Http.StatusChangedEventArgs e)
{
    if (_sendingData)
    {
        _uploadStatus.Text = string.Format("{0}{1}", _uploadStatus.Text, Environment.NewLine);
        _sendingData = false;
    }
            
    // show current upload status in the status string
    _uploadStatus.Text = string.Format("{0}{1}{2}", _uploadStatus.Text, e.StatusString, Environment.NewLine);
}
            
// HTTP uploading is in progress.
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, 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);
}
            
// HTTP uploading is completed.
private void _httpUpload_Completed(object sender, Vintasoft.Twain.ImageUploading.Http.CompletedEventArgs e)
{
    _uploadStatus.Text = string.Format("{0}{1}", _uploadStatus.Text, Environment.NewLine);
            
    // if no errors
    if (e.ErrorCode == 0)
    {
        // if server returned "OK" status
        if (e.ResponseCode == HttpStatusCode.OK)
        {
            _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);
        }
        // if server returned NOT "OK" status
        else
        {
            _uploadStatus.Text = string.Format("{0}Response code: {1}{2}", _uploadStatus.Text, e.ResponseCode, Environment.NewLine);
            _uploadStatus.Text = string.Format("{0}{1}", 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, Environment.NewLine);
}
Requirements

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

See Also

Reference

HttpUpload Class
HttpUpload Members