彩票走势图

使用Dynamsoft存储和检索SQL Server中的扫描图像

原创|使用教程|编辑:龚雪|2013-11-19 09:08:44.000|阅读 224 次

概述:存储和检索数字化文档是任何文档管理工作流过程的一个重要特征。本文我们将介绍如何使用ASP.NET Web程序中的文档扫描功能将扫描文件保存为PDF文档,并保存至SQL Server数据库中。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

存储和检索数字化文档是任何文档管理工作流过程的一个重要特征。本文我们将介绍如何使用ASP.NET Web程序中的文档扫描功能将扫描文件保存为PDF文档,并保存至SQL Server数据库中。

在本文中我们将使用Dynamic Web TWAIN来加快文档扫描,上传和显示功能的开发进程。

Dynamic Web TWAIN主要功能:

  • Windows或Mac上兼容Firefox,Chrome,Opera和Safari等主流浏览器
  • 支持从扫描仪或者其他TWAIN兼容设备中扫描图像
  • BMP,JPEG,PNG,单/多页 PDF和单/多页TIFF
  • 支持HTTPS上传
  • 支持cookie和会话的集成

文件扫描

由于Dynamic Web TWAIN是一个客户端SDK,我们将使用JavaScript来调用它的方法/属性。有了扫描识别工具Dynamic Web TWAIN,你可以自定义扫描设置,如分辨率,像素类型,亮度,对比度,页面大小等。本文,我们将重点介绍如何从SQL Server中储存和检索图像。为此,我们将只包括一个简单的扫描过程。

function acquireImage() {
if (_divDWTSourceContainerID == "")
DWObject.SelectSource();
else
DWObject.SelectSourceByIndex(document.getElementById(_divDWTSourceContainerID).selectedIndex); //select a TWAIN scanner
DWObject.CloseSource(); //make sure the source is closed before using it
DWObject.OpenSource();
DWObject.IfShowUI = document.getElementById("ShowUI").checked; //show or hide the user interface of the TWAIN scanner

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 acquired images, B/W, gray or color
DWObject.Resolution = document.getElementById("Resolution").value; //set the resolution
DWObject.IfFeederEnabled = document.getElementById("ADF").checked; //scan images from auto feeder
DWObject.IfDuplexEnabled = document.getElementById("Duplex").checked; //enable duplex scanning
appendMessage("Pixel Type: " + DWObject.PixelType + "<br />Resolution: " + DWObject.Resolution + "<br />");

DWObject.IfDisableSourceAfterAcquire = true;
DWObject.AcquireImage(); //start document scanning
}

将扫描图像作为个多页PDF保存至SQL Server

 扫描完成后,你可以将图像保存为多种格式:BMP、PNG、JPG、TIF、PDF、多页PDF或多页TIF。在这个例子中,我们将扫描图像保存为一个多页PDF文件。代码如下所示:

function btnUpload_onclick() {
if (!checkIfImagesInBuffer()) {
return;
}
var i, strHTTPServer, strActionPage, strImageType;
_txtFileName.className = "";
if (!strre.test(_txtFileName.value)) {
_txtFileName.className += " invalid";
_txtFileName.focus();
appendMessage("Please input file name.<br />Currently only English names are allowed.<br />");
return;
}
//DWObject.MaxInternetTransferThreads = 5;
strHTTPServer = _strServerName;
DWObject.HTTPPort = _strPort;
var CurrentPathName = unescape(location.pathname); // get current PathName in plain ASCII
var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);
strActionPage = CurrentPath + _strActionPage; // the aspx page for receiving image data on the server side
var redirectURLifOK = CurrentPath + "online_demo_list.aspx";
var uploadfilename = _txtFileName.value + "." + document.getElementsByName("ImageType").item(i).value;

DWObject.HTTPUploadAllThroughPostAsPDF(
strHTTPServer,
strActionPage,
uploadfilename
); //upload images as multi-page PDF file

_strTempStr = _strTempStr + "Upload: ";
if (checkErrorString()) {
if (strActionPage.indexOf("SaveToFile") != -1)
alert(DWObject.ErrorString)//if save to file.
else
window.location = redirectURLifOK;
}
}

Action Page- SaveToDB.aspx

动作页面用于接收来自扫描图像的图像数据。

<%@ Page Language="C#"%>
<%
try
{
String strImageName;
int iFileLength;
HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile uploadfile = files["RemoteFile"];
strImageName = uploadfile.FileName;
iFileLength = uploadfile.ContentLength;

Byte[] inputBuffer = new Byte[iFileLength];
System.IO.Stream inputStream;

inputStream = uploadfile.InputStream;
inputStream.Read(inputBuffer, 0, iFileLength);

String strConnString;

strConnString = Common.DW_ConnString;

System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(strConnString);

String SqlCmdText = "INSERT INTO " + Common.DW_SaveTable + " (strImageName,imgImageData) VALUES (@ImageName,@Image)";
System.Data.SqlClient.SqlCommand sqlCmdObj = new System.Data.SqlClient.SqlCommand(SqlCmdText, sqlConnection);

sqlCmdObj.Parameters.Add("@Image", System.Data.SqlDbType.Binary, iFileLength).Value = inputBuffer;
sqlCmdObj.Parameters.Add("@ImageName", System.Data.SqlDbType.VarChar, 255).Value = strImageName;

sqlConnection.Open();
sqlCmdObj.ExecuteNonQuery();
sqlConnection.Close();
}
catch
{
}
%>

从Web页面上显示数据库中检索PDF文件

Online_demo_view.aspx用于显示PDF文档

setTimeout(function () {
var CurrentPathName = unescape(location.pathname); // get current PathName in plain ASCII
var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);
var strActionPage = CurrentPath + "online_demo_download.aspx"; //the ActionPage's file path

strHTTPServer = location.hostname;
DWObject.HTTPPort = location.port==""?80:location.port;
var downloadsource = strActionPage +
"?iImageIndex=<%=strImageID%>&ImageName=<%=strImageName%>&ImageExtName=<%=strImageExtName%>";
DWObject.HTTPDownloadEx(strHTTPServer, downloadsource,<%=strImageFileType %>);
}, 500);

online_demo_download.aspx用于检索数据库中的图像

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

<%
String strExc = "";
try
{
//Get the image data from the database
HttpRequest request = HttpContext.Current.Request;

String strImageName;
String strImageExtName;
String strImageID;

strImageName = request["ImageName"];
strImageExtName = request["ImageExtName"];
strImageID = request["iImageIndex"];

String strConnString;

strConnString = Common.DW_ConnString;

System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(strConnString);
System.Data.SqlClient.SqlCommand sqlCmdObj = new System.Data.SqlClient.SqlCommand("SELECT imgImageData FROM " +
Common.DW_SaveTable + " WHERE iImageID= " + strImageID, sqlConnection);

sqlConnection.Open();

System.Data.SqlClient.SqlDataReader sdrRecordset = sqlCmdObj.ExecuteReader();

sdrRecordset.Read();

long iByteLength;
iByteLength = sdrRecordset.GetBytes(0, 0, null, 0, int.MaxValue);

byte[] byFileData = new byte[iByteLength];

sdrRecordset.GetBytes(0, 0, byFileData, 0, Convert.ToInt32(iByteLength));

sdrRecordset.Close();
sqlConnection.Close();

sdrRecordset = null;
sqlConnection = null;

Response.Clear();
Response.Buffer = true;

if (strImageExtName == "bmp")
{
Response.ContentType = "image/bmp";
}
else if (strImageExtName == "jpg")
{
Response.ContentType = "image/jpg";
}
else if (strImageExtName == "tif")
{
Response.ContentType = "image/tiff";
}
else if (strImageExtName == "png")
{
Response.ContentType = "image/png";
}
else if (strImageExtName == "pdf")
{
Response.ContentType = "application/pdf";
}

try
{
String fileNameEncode;
fileNameEncode = HttpUtility.UrlEncode(strImageName, System.Text.Encoding.UTF8);
fileNameEncode = fileNameEncode.Replace("+", "%20");
String appendedheader = "attachment;filename=" + fileNameEncode;
Response.AppendHeader("Content-Disposition", appendedheader);

Response.OutputStream.Write(byFileData, 0, byFileData.Length);
}
catch (Exception exc)
{
strExc = exc.ToString();
DateTime d1 = DateTime.Now;
string logfilename = d1.Year.ToString() + d1.Month.ToString() + d1.Day.ToString() +
d1.Hour.ToString() + d1.Minute.ToString() + d1.Second.ToString() + "log.txt";
String strField1Path = HttpContext.Current.Request.MapPath(".") + "/" + logfilename;
if (strField1Path != null)
{
System.IO.StreamWriter sw1 = System.IO.File.CreateText(strField1Path);
sw1.Write(strExc);
sw1.Close();
}
Response.Flush();
Response.Close();
}
}
catch (Exception ex)
{
strExc = ex.ToString();
DateTime d1 = DateTime.Now;
string logfilename = d1.Year.ToString() + d1.Month.ToString() + d1.Day.ToString() +
d1.Hour.ToString() + d1.Minute.ToString() + d1.Second.ToString() + "log.txt";
String strField1Path = HttpContext.Current.Request.MapPath(".") + "/" + logfilename;
if (strField1Path != null)
{
System.IO.StreamWriter sw1 = System.IO.File.CreateText(strField1Path);
sw1.Write(strExc);
sw1.Close();
}
Response.Write(strExc);
}
%>

标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP