getting "Indices are out of range" error from dynamic web Twain API

404 views Asked by At

I'm getting indices are out of range error from the "convertToBlob" method of dynamic Web twain, Im using it to scan documents via scanner, can anyone help?

  dwObject.ConvertToBlob([0], EnumDWT_ImageType.IT_JPG, (result) => {
        const data = new File([result], 'Scan.jpeg', result);
        let attachmentsBlob = [];
        let attachmentObject = [];
        directAccess ? attachmentObject.push({ attachmentName: "Scan.jpeg", attachmentType: parentType, documentType: type }) : attachmentObject.push({ attachmentName: "Scan.jpeg", attachmentType: type });
        attachmentsBlob.push(data);
        this.customerAttachment(attachmentObject, attachmentsBlob).subscribe((res: any) => {
          if (res['code'] === '0000') {
            this.toastr.success("Attachement uploaded Successfully");
            if (directAccess) {
              form.patchValue(res.data[0]);
            } else {
              let scan = form.controls.customerAttachments.value;
              scan.push(res.data[0]);
              form.controls.customerAttachments.setValue(scan);
            }

          } else if (res['code'] === '0101') {
            this.toastr.error('Error', res['data']['message']);
          }
        });
      }, (errorCode, errorString) => {
        this.toastr.error(errorString);
      });
1

There are 1 answers

0
yushulx On

Not sure how you wrote your code. As long as you have scanned documents in memory, you won't get this issue.

Here is a simple example of Dynamic Web TWAIN v17.1 (the namespace is different from yours), demonstrating how to use ConvertToBlob:

<!DOCTYPE html>
<html>

<head>
    <title>Use Dynamic Web TWAIN to Scan</title>
    <script type="text/javascript" src="Resources/dynamsoft.webtwain.initiate.js"></script>
    <script type="text/javascript" src="Resources/dynamsoft.webtwain.config.js"></script>
</head>

<body>
    <select size="1" id="source" style="position: relative; width: 220px;"></select>
    <input type="button" value="Scan" onclick="AcquireImage();" />

    <div id="dwtcontrolContainer"></div>

    <script type="text/javascript">
        Dynamsoft.DWT.RegisterEvent('OnWebTwainReady', Dynamsoft_OnReady);

        var DWObject;

        function Dynamsoft_OnReady() {
            DWObject = Dynamsoft.DWT.GetWebTwain('dwtcontrolContainer');
            if (DWObject) {
                var count = DWObject.SourceCount;
                for (var i = 0; i < count; i++)
                    document.getElementById("source").options.add(new Option(DWObject.GetSourceNameItems(i), i));
            }
        }
        function AcquireImage() {
            if (DWObject) {
                var OnAcquireImageSuccess = function () {
                    DWObject.ConvertToBlob([0], Dynamsoft.DWT.EnumDWT_ImageType.IT_JPG, (result) => {
                        const data = new File([result], 'Scan.jpeg', result);
                        console.log(data);
                    }, (errorCode, errorString) => {
                        this.toastr.error(errorString);
                    });
                    DWObject.CloseSource();
                };

                DWObject.SelectSourceByIndex(document.getElementById("source").selectedIndex); 
                DWObject.OpenSource();
                DWObject.IfDisableSourceAfterAcquire = true;
                DWObject.AcquireImage(OnAcquireImageSuccess, () =>{});
            }
        }
    </script>
</body>

</html>