You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Dynamic Web TWAIN的convertToBlob方法报索引超出范围错误求助

Dynamic Web TWAIN ConvertToBlob 「Indices are out of range」报错解决方案

报错根因

该报错的核心原因是你传入ConvertToBlob方法的图像索引[0]不存在于Dynamic Web TWAIN实例的图像缓存队列中,常见触发场景如下:

  • 异步扫描任务未完成就提前调用转换方法,此时缓存队列为空
  • 转换前已调用RemoveImage()/RemoveAllImages()等方法清空了缓存
  • 索引值大于当前缓存队列的最大索引(缓存N张图时,有效索引范围是0到N-1)

修复方案

第一步:增加缓存有效性校验

调用ConvertToBlob前先通过HowManyImagesInBuffer属性获取当前缓存的图像总数,确认索引在有效范围内再执行转换逻辑。

第二步:确保转换逻辑在扫描完成回调中触发

不要在扫描触发的同步代码后直接调用转换,需绑定扫描完成事件(如OnPostTransferAll),等所有扫描页存入缓存后再执行转换。

修复后代码示例

// 建议在扫描完成的回调内执行如下逻辑
const imageIndex = 0;
// 先校验缓存是否有效
if (dwObject.HowManyImagesInBuffer > imageIndex) {
  dwObject.ConvertToBlob([imageIndex], 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);
      });
} else {
  this.toastr.error("当前无有效扫描图像可转换");
}

额外注意事项

  • 若每次扫描仅需处理最新扫描的图像,可在扫描触发前调用dwObject.RemoveAllImages()清空历史缓存,避免索引错位
  • 多页扫描场景下可按需传入对应的索引数组,只要所有索引均小于HowManyImagesInBuffer即可正常转换

内容的提问来源于stack exchange,提问作者Ayub Jamal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 12:36:05