Dynamic Web TWAIN: How to upload multiple files at a time

Scenario:
After scanning multiple files, you might want to upload them one by one as individual images. Before version 13.1, you have to call the upload method(s) multiple times. In v13.1, you can do this in one go.

Solution:
You can use the methods ConvertToBlob and HTTPUpload  to achieve this.

Steps:

1. In JS, write code similar to the following

var i = 0;
DWObject.ClearAllHTTPFormField();
DWObject.SetHTTPFormField("UploadedImagesCount", DWObject.HowManyImagesInBuffer);
function asyncFailureFunc(errorCode, errorString) {
 alert("ErrorCode: " + errorCode + "\r" + "ErrorString:" + errorString);
}
var convertImage = function (_index) {
 DWObject.ConvertToBlob([_index], EnumDWT_ImageType.IT_JPG,
 function (result) {
 DWObject.SetHTTPFormField('image_' + _index, result, 'JPG_image_' + _index);
 i++;
 if (i < DWObject.HowManyImagesInBuffer) {
 convertImage(i);
 }
 else {
 DWObject.HTTPUpload("http://localhost:1242/SaveToFiles.aspx", OnHttpUploadSuccess, OnHttpUploadFailure);
 }
 }, asyncFailureFunc);
}
convertImage(0);

2. On the server, add an action page to process the uploaded data

<%@ Page Language="C#" %>

<%
try
{
    String strImageName;
    String strInfo = HttpContext.Current.Request["UploadedImagesCount"];
    short uploadedImagesCount = Convert.ToInt16(strInfo);
    HttpFileCollection files = HttpContext.Current.Request.Files;
    for (short i = 0; i < uploadedImagesCount; i++)
    {
        HttpPostedFile uploadfile = files["image_" + i.ToString()];
        strImageName = uploadfile.FileName;

        uploadfile.SaveAs(Server.MapPath(".") + "\\UploadedImages\\" + strImageName + ".jpg");
    }
}
catch
{
}
%>
Is this page helpful?

Leave a Reply

Your email address will not be published.