65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Dynamic Web TWAIN 在 ASP.NET MVC 应用中扫描和上传文档

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2013年7月10日

CPOL

3分钟阅读

viewsIcon

62435

downloadIcon

4365

本文将向您展示如何在 ASP.NET 模型视图控制器 (MVC) 3 Web 应用程序中扫描和上传文档。我们将使用 Dynamic Web TWAIN 扫描 SDK,它被广泛用于加速此类应用程序的开发和部署。

引言

越来越多的组织需要文档影像解决方案,以将纸质文档转换为电子/数字化文档。组织这样做的主要目的是为了更容易、更有效地访问和管理其记录和数据。从基于纸质到基于数字的文档管理的这种转变,正迅速成为所有组织的必备条件。

本文将向您展示如何在 ASP.NET 模型视图控制器 (MVC) 3 Web 应用程序中扫描和上传文档。我们将使用Dynamic Web TWAIN 扫描 SDK,它被广泛用于加速此类应用程序的开发和部署。 

Dynamic Web TWAIN 是一个 TWAIN 扫描 SDK,专门为 Web 应用程序优化。该 SDK 允许您在客户端机器上通过 JavaScript 与扫描仪、数码相机和其他 TWAIN 兼容设备进行交互。您可以将扫描的文档保存/上传到本地或服务器磁盘、数据库或 SharePoint。

Using the Code

如果您有兴趣使用 SDK 来使您的文档扫描应用程序的部署过程更快,您可以下载 Dynamic Web TWAIN 30 天免费试用版。安装后,您可以在“\Dynamsoft\Dynamic Web TWAIN 9.0 Trial\Resources.”下找到以下文件。

  • DynamicWebTWAIN.cab/ DynamicWebTWAINx64.cab – 适用于 Internet Explorer (IE) 32 位和 64 位的 ActiveX 控件版
  • DynamicWebTWAINPlugIn.msi - 适用于 Chrome、Firefox、Safari on Windows 的插件版
  • DynamicWebTWAINMacEditionTrial.pkg – 适用于 Mac OS X 上 Chrome、Firefox、Safari 的 Mac 版。

要将 Dynamic Web TWAIN 嵌入到您的 MVC 应用程序中,请将上述 4 个 Dynamic Web TWAIN 插件文件复制并粘贴到您的 MVC 应用程序的Content 文件夹中。以下是截图和代码,以帮助您入门。

文档扫描视图

如下所示,我们在 Index 页面中添加了一个“扫描”选项卡。单击“扫描”后,用户将使用 Dynamic Web TWAIN 插件看到 Scan.aspx 页面。


在 JavaScript 中启动 Dynamic Web TWAIN

function DW_CreateControl(bNeebBack) {
    var objString = "";
    var DWTContainer;

    // For IE, render the ActiveX Object
    if (DW_InIE) {
        objString = "<object id='" + DW_ObjectName + "' style='width:" + DW_Width + "px;height:" + DW_Height + "px'";

        if (DW_InWindowsX86)
            objString += "codebase='" + DW_CABX86Path + "#version=" + DW_VersionCode + "' "; //load 32 bit CAB file for 32 bit Windows
        else
            objString += "codebase='" + DW_CABX64Path + "#version=" + DW_VersionCode + "' "; //load 64 bit CAB file for 64 bit Windows

        var temp = DW_IsTrial ? DW_TRAILCLASSID : DW_FULLCLASSID;
        objString += " classid='clsid:" + temp + "' viewastext>";

        objString += " <param name='Manufacturer' value='DynamSoft Corporation' />";
        objString += " <param name='ProductFamily' value='" + DW_ProductName + "' />";
        objString += " <param name='ProductName' value='" + DW_ProductName + "' />";
        //objString += " <param name='wmode' value='transparent'/>  ";
        objString += " </object>";
    }   
    // For non-IE browsers, render the embed object  
    else {
        objString = " <embed id='" + DW_ObjectName + "'style='display: inline; width:" + DW_Width + "px;height:" + DW_Height + "px' id='" + DW_ObjectName + "' type='" + DW_MIMETYPE + "'";
        objString += " OnPostTransfer='Dynamsoft_OnPostTransfer' OnPostAllTransfers='Dynamsoft_OnPostAllTransfers'";
        objString += " OnMouseClick='Dynamsoft_OnMouseClick'  OnPostLoad='Dynamsoft_OnPostLoadfunction'";
        objString += " OnImageAreaSelected = 'Dynamsoft_OnImageAreaSelected'";
        objString += " OnImageAreaDeSelected = 'Dynamsoft_OnImageAreaDeselected'";
        objString += " OnMouseDoubleClick = 'Dynamsoft_OnMouseDoubleClick'";
        objString += " OnMouseRightClick = 'Dynamsoft_OnMouseRightClick'";
        objString += " OnTopImageInTheViewChanged = 'Dynamsoft_OnTopImageInTheViewChanged'";
        if (DW_InWindows)
            objString += " pluginspage='" + 
else
            objString += " pluginspage='" + DW_PKGPath + "'></embed>"; //load the Mac edition for Chrome, Firefox, Safari on Mac
    }
   

    DWTContainer = document.getElementById(DW_DWTContainerID);
    DWTContainer.innerHTML = objString;
    DWObject = document.getElementById(DW_ObjectName);
}

扫描文档

使用 Dynamic Web TWAIN 扫描 SDK 很容易从扫描仪捕获图像。以下是设置像素类型、分辨率、选择 ADF/双面扫描以及进行扫描的 JavaScript 代码。

function AcquireImageInner() {
    if (DW_DWTSourceContainerID == "")
        DWObject.SelectSource();
    else
        DWObject.SelectSourceByIndex(document.getElementById(DW_DWTSourceContainerID).selectedIndex); //select from the available TWAIN devices
    DWObject.CloseSource();
    DWObject.OpenSource();
    DWObject.IfShowUI = document.getElementById("ShowUI").checked;

    var i;
    for (i = 0; i < 3; i++) {
        if (document.getElementsByName("PixelType").item(i).checked == true)
            DWObject.PixelType = i;
    } //set the pixel type of the image
    DWObject.Resolution = Resolution.value;
    DWObject.IfFeederEnabled = document.getElementById("ADF").checked; //whether scan from ADF or flatbed
    DWObject.IfDuplexEnabled = document.getElementById("Duplex").checked; //whether do duplex scan

    DWObject.IfDisableSourceAfterAcquire = true;
    DWObject.AcquireImage();  //acquire images from the scanner
}

上传文档

将图像扫描到 Dynamic Web TWAIN 后,您可以使用 HTTP Post 方法将扫描的图像上传到 Web 服务器。

var DW_ActionPage = "SaveToFile"; //call SaveToFile controller

function btnUpload_onclick(){
    var i, strHTTPServer, strActionPage, strImageType;
    strHTTPServer = DW_ServerName;
    DWObject.HTTPPort = DW_strPort;
    var CurrentPathName = unescape(location.pathname);  // get current PathName in plain ASCII  
    var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);           
    strActionPage = CurrentPath + DW_ActionPage; //the ActionPage's file path
   
    for(i=0;i<4;i++){
        if(document.getElementsByName("ImageType").item(i).checked == true){
            strImageType  = i + 1;
            break;
        }
    } //choose the image type, JPEG, TIFF, PNG or PDF
    var uploadfilename = txt_fileName.value + "." + document.getElementsByName("ImageType").item(i).value;
   
    if (strImageType == 2 && document.getElementById("MultiPageTIFF").checked) {
        if ((DWObject.SelectedImagesCount == 1) || (DWObject.SelectedImagesCount == DWObject.HowManyImagesInBuffer)) {
            DWObject.HTTPUploadAllThroughPostAsMultiPageTIFF(
                strHTTPServer,
                strActionPage,
                uploadfilename
            );
{
            DWObject.HTTPUploadThroughPostAsMultiPageTIFF(
                strHTTPServer,
                strActionPage,
                uploadfilename
            );
        }
    } //whether to upload the images as a multi-page TIFF file
  
    else if (strImageType == 4 && document.getElementById("MultiPagePDF").checked) {
        if ((DWObject.SelectedImagesCount == 1) || (DWObject.SelectedImagesCount == DWObject.HowManyImagesInBuffer)) {
            DWObject.HTTPUploadAllThroughPostAsPDF(
                strHTTPServer,
                strActionPage,
                uploadfilename
            );
        }
        else {
            DWObject.HTTPUploadThroughPostAsMultiPagePDF(
                strHTTPServer,
                strActionPage,
                uploadfilename
            );
        }
    } //whether to upload the images as a multi-page PDF file
    else {
        DWObject.HTTPUploadThroughPostEx(
            strHTTPServer,
            DWObject.CurrentImageIndexInBuffer,
            strActionPage,
            	          uploadfilename,            
            strImageType
        );
    }

Controller.cs

public String SaveToFile()
        {
            String strImageName;
            HttpFileCollectionBase files = Request.Files;
            HttpPostedFileBase uploadfile = files["RemoteFile"];
            strImageName = uploadfile.FileName;

            uploadfile.SaveAs(Server.MapPath("~") + "\\UploadedImages\\" + strImageName);

            return "";
        }

在 Web 服务器上运行或部署应用程序

完整的源代码可以从本文中下载。

有一个“Scripts\ProductKey.js”文件,其中包含一个临时的试用产品密钥。如果运行示例代码时收到许可证错误,您可以从 Dynamsoft 的网站下载 Dynamic Web TWAIN 以获取有效的试用许可证:Dynamic Web TWAIN 30 天免费试用下载

要从不同的客户端机器测试文档扫描和上传,您可以简单地将示例代码文件复制到您的 Web 服务器(IIS、Apache 或 Tomcat)。用户只需在首次访问扫描页面时在浏览器中下载并安装 ActiveX/插件。 

在线演示也供您参考:Dynamic Web TWAIN 在线演示

支持

您的组织目前是否正在进行数字化文档的项目?您是否部署了 SDK,它如何帮助了您?请在评论中或通过联系我们告诉我们。如果您需要任何帮助来运行此示例代码,也可以联系我们。为此,请通过电子邮件联系我们,地址为support@dynamsoft.com。对于定价或许可问题,请致电 1-877-605-5491 或通过电子邮件向我们的销售团队发送邮件,地址为sales@dynamsoft.com

使用 Dynamic Web TWAIN 在 ASP.NET MVC 应用中扫描和上传文档 - CodeProject - 代码之家
© . All rights reserved.