原創|使用教程|編輯:龔雪|2013-11-19 09:08:44.000|閱讀 261 次
概述:存儲和檢索數字化文檔是任何文檔管理工作流過程的一個重要特征。本文我們將介紹如何使用ASP.NET Web程序中的文檔掃描功能將掃描文件保存為PDF文檔,并保存至SQL Server數據庫中。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
存儲和檢索數字化文檔是任何文檔管理工作流過程的一個重要特征。本文我們將介紹如何使用ASP.NET Web程序中的文檔掃描功能將掃描文件保存為PDF文檔,并保存至SQL Server數據庫中。
在本文中我們將使用Dynamic Web TWAIN來加快文檔掃描,上傳和顯示功能的開發進程。
文件掃描
由于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@fc6vip.cn
文章轉載自:慧都控件網