VintaSoft Twain .NET SDK 15.5: Documentation for Web developer
In This Topic
Upload TWAIN/WIA/SANE/eSCL acquired images to HTTP(S) server
In This Topic
Vintasoft Web TWAIN service allows to save images, which are received from TWAIN/WIA/SANE/eSCL image scanner, to a file (PDF, TIFF, JPEG, PNG, etc) in memory and get the saved file as a Base64 string. Saved file, as a Base64 string, can be uploaded to HTTP(S) server.

Here is JavaScript code that demonstrates how to save images, which are acquired from TWAIN/WIA/SANE/eSCL image scanner, to a PDF file in memory, get PDF file as a Base64 string and upload PDF file to HTTPS server:
// acquire images from TWAIN/WIA/SANE/eSCL image scanner, save images to PDF file in memory and upload PDF file to HTTPS server
__acquireImagesFromTwainScannerAndUploadImagesToHttpsServerAsPdfDocument();



/**
 * Acquires images from TWAIN/WIA/SANE/eSCL image scanner, saves images to PDF file in memory and uploads PDF file to HTTPS server.
 */
function __acquireImagesFromTwainScannerAndUploadImagesToHttpsServerAsPdfDocument() {
    // register the evaluation version of VintaSoft Web TWAIN service
    // please read how to get evaluation license in documentation: https://www.vintasoft.com/docs/vstwain-dotnet-web/Licensing-Twain_Web-Evaluation.html
    Vintasoft.Twain.WebTwainGlobalSettingsJS.register('REG_USER', 'REG_URL', 'REG_CODE', 'EXPIRATION_DATE');

    // URL to the VintaSoft Web TWAIN service
    var serviceUrl = 'https://localhost:25329/api/VintasoftTwainApi';
    // a Web API controller that allows to work with TWAIN/WIA/SANE/eSCL/eSCL devices
    var twainService = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);

    // TWAIN/WIA/SANE/eSCL device manager
    var deviceManager = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);

    // the default settings of device manager
    var deviceManagerInitSetting = new Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS();

    var device = null;
    try {
        // open device manager
        deviceManager.open(deviceManagerInitSetting);

        // get the default TWAIN/WIA/SANE/eSCL device
        device = deviceManager.get_DefaultDevice();

        // open device without UI
        device.open(false);

        // a collection that stores images, which are acquired from TWAIN/WIA/SANE/eSCL devices and stored in memory of VintaSoft Web TWAIN service
        var acquiredImages = new Vintasoft.Twain.WebAcquiredImageCollectionJS(deviceManager);

        var acquireModalState;
        do {
            // do one step of modal image acquisition process
            var acquireModalResult = device.acquireModalSync();
            // get state of image acquisition
            acquireModalState = acquireModalResult.get_AcquireModalState().valueOf();

            switch (acquireModalState) {
                case 2:   // image is acquired
                    // get acquired image
                    var acquiredImage = acquireModalResult.get_AcquiredImage();
                    // add acquired image to the collection of acquired images
                    acquiredImages.add(acquiredImage);
                    break;

                case 4:   // image scan is failed
                    alert(acquireModalResult.get_ErrorMessage());
                    break;

                case 9:   // image scan is finished
                    var imageIds = []
                    // for each acquired image
                    for (var i = 0; i < acquiredImages.get_Count(); i++)
                        // add image identifier to array of image identifiers
                        imageIds.push(acquiredImages.get_Item(i).get_Id());

                    // save images to an image file and return file data as a Base64 string
                    var imageFileAsBase64String = acquiredImages.getAsBase64String('scanResult.pdf', imageIds);

                    // clear image collection (delete images from memory of VintaSoft Web TWAIN service) because images are not necessary anymore
                    acquiredImages.clear();

                    // create XMLHttpRequest
                    var req = new XMLHttpRequest();
                    // create form data
                    var formData = new FormData();
                    // add file name to the form data
                    formData.append('fileName', 'scanResult.pdf');
                    // add file data to the form data
                    formData.append('imageFileAsBase64String', imageFileAsBase64String);
                    // open request
                    req.open('POST', 'https://demos.vintasoft.com/AspNetMvcTwainScanningDemo/api/ImageUploadApi');
                    // subscribe to the 'onreadystatechange' event of request
                    req.onreadystatechange = __uploadImages_readyStateChanged;
                    // send request
                    req.send(formData);
                    break;
            }
        }
        while (acquireModalState !== 0);
    }
    catch (ex) {
        alert(ex);
    }
    finally {
        if (device != null) {
            // close the device
            device.close();
        }
        // close the device manager
        deviceManager.close();
    }
}

/**
 * State of HTTP uploading request is changed.
 */
function __uploadImages_readyStateChanged(data) {
    var httpRequest = data.currentTarget;
    if (httpRequest.readyState == 4) {
        // if image image is uploaded successfully and server returned status 20X
        if (httpRequest.status == 200 || httpRequest.status == 204) {
            alert('Image uploading is finished successfully.');
        }
        // if image image is uploaded successfully and server returned status 0 (CORS request)
        else if (httpRequest.status == 0) {
            alert('Image uploading is finished successfully.');
        }
        else {
  
            alert('Image uploading is failed.');
        }
    }
}



Here is TypeScript code that demonstrates how to save images, which are acquired from TWAIN/WIA/SANE/eSCL image scanner, to a PDF file in memory, get PDF file as a Base64 string and upload PDF file to HTTPS server:
import { Component } from '@angular/core';

@Component({
  selector: 'twain-scanning-demo',
  templateUrl: './twain-scanning-demo.component.html'
})
export class TwainScanningDemoComponent {

  ngOnInit() {
    // acquire images from TWAIN/WIA/SANE/eSCL image scanner and process acquired images
    this.__acquireImagesFromTwainScannerAndProcessAcquiredImages();
  }


  /**
   * Acquires images from TWAIN/WIA/SANE/eSCL image scanner and process acquired images.
   */
  __acquireImagesFromTwainScannerAndProcessAcquiredImages() {
    // register the evaluation version of VintaSoft Web TWAIN service
    // please read how to get evaluation license in documentation: https://www.vintasoft.com/docs/vstwain-dotnet-web/Licensing-Twain_Web-Evaluation.html
    Vintasoft.Twain.WebTwainGlobalSettingsJS.register('REG_USER', 'REG_URL', 'REG_CODE', 'EXPIRATION_DATE');

    // URL to the VintaSoft Web TWAIN service
    let serviceUrl: string = 'https://localhost:25329/api/VintasoftTwainApi';
    // a Web API controller that allows to work with TWAIN/WIA/SANE/eSCL/eSCL devices
    let twainService: Vintasoft.Shared.WebServiceControllerJS = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);

    // TWAIN/WIA/SANE/eSCL device manager
    let deviceManager: Vintasoft.Twain.WebTwainDeviceManagerJS = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);

    // the default settings of device manager
    let deviceManagerInitSetting: Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS = new Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS();

    try {
      // open device manager
      deviceManager.open(deviceManagerInitSetting);
    }
    catch (ex) {
      alert(ex);
      return;
    }

    let device: Vintasoft.Twain.WebTwainDeviceJS = null;
    try {
      // get the default TWAIN/WIA/SANE/eSCL device
      device = deviceManager.get_DefaultDevice();

      // open device without UI
      device.open(false);

      // a collection that stores images, which are acquired from TWAIN/WIA/SANE/eSCL devices and stored in memory of VintaSoft Web TWAIN service
      let acquiredImages: Vintasoft.Twain.WebAcquiredImageCollectionJS = new Vintasoft.Twain.WebAcquiredImageCollectionJS(deviceManager);

      let acquireModalState: number;
      do {
        // do one step of modal image acquisition process
        let acquireModalResult: Vintasoft.Twain.WebTwainDeviceAcquireModalResultJS = device.acquireModalSync();
        // get state of image acquisition
        acquireModalState = acquireModalResult.get_AcquireModalState().valueOf() as number;

        switch (acquireModalState) {
          case 2:   // image is acquired
            // get acquired image
            let acquiredImage: Vintasoft.Twain.WebAcquiredImageJS = acquireModalResult.get_AcquiredImage();
            // add acquired image to the collection of acquired images
            acquiredImages.add(acquiredImage);
            break;

          case 4:   // image scan is failed
            alert(acquireModalResult.get_ErrorMessage());
            break;

          case 9:   // image scan is finished
            let imageIds: string[] = []
            // for each acquired image
            for (let i: number = 0; i < acquiredImages.get_Count(); i++)
              // add image identifier to array of image identifiers
              imageIds.push(acquiredImages.get_Item(i).get_Id());

            // save images to an image file and return file data as a Base64 string
            let imageFileAsBase64String: string = acquiredImages.getAsBase64String('scanResult.pdf', imageIds);

            // clear image collection (delete images from memory of VintaSoft Web TWAIN service) because images are not necessary anymore
            acquiredImages.clear();

            // create XMLHttpRequest
            let req: XMLHttpRequest = new XMLHttpRequest();
            // create form data
            let formData: FormData = new FormData();
            // add file name to the form data
            formData.append('fileName', 'scanResult.pdf');
            // add file data to the form data
            formData.append('imageFileAsBase64String', imageFileAsBase64String);
            // open request
            req.open('POST', 'https://demos.vintasoft.com/AspNetMvcTwainScanningDemo/api/ImageUploadApi');
            // subscribe to the 'onreadystatechange' event of request
            req.onreadystatechange = this.__uploadImages_readyStateChanged;
            // send request
            req.send(formData);
            break;
        }
      }
      while (acquireModalState !== 0);
    }
    catch (ex) {
      alert(ex);
    }
    finally {
      if (device != null) {
        // close the device
        device.close();
      }
      // close the device manager
      deviceManager.close();
    }
  }

  /**
   * State of HTTP uploading request is changed.
   */
  __uploadImages_readyStateChanged(data: any) {
    let httpRequest: XMLHttpRequest = data.currentTarget;
    if (httpRequest.readyState == 4) {
      // if image image is uploaded successfully and server returned status 20X
      if (httpRequest.status == 200 || httpRequest.status == 204) {
        alert('Image uploading is finished successfully.');
      }
      // if image image is uploaded successfully and server returned status 0 (CORS request)
      else if (httpRequest.status == 0) {
        alert('Image uploading is finished successfully.');
      }
      else {
        alert('Image uploading is failed.');
      }
    }
  }

}