Dynamic Web TWAIN: How to download images directly from Database?

Scenario:
"Once a scan is added into the database, can it later be brought out of the DB and back into the Web TWAIN for viewing?"

Solution:
You can use the method HTTPDownloadEx to achieve this.

Steps:
1. On the server, add an action page to fetch the image data from the database and send it back to the client browser.

Here we take C# and MSSQL as an example:

<%@ Page Language="C#"%>
<%
   ??/Get the image data from the database
   try//Send the image data to the client as stream    |
   {
        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)
    {
        Response.Flush();
        Response.Close();
    }
catch (Exception)
{ }
%>


2. On the client side, use the method HTTPDownloadEx to download the image. Put the action page DownloadFromDB.aspx as the source of the image (as the 2nd parameter "String HTTPRemoteFile"):

//downloadsource should be the correct path for 
//the page DownloadFromDB.aspx plus any necessary
//parameters needed for fetching the image(s)
var downloadsource = DownloadFromDB.aspx + 
"?iImageIndex=<%=strImageID%>&ImageName=<%=strImageName%>&ImageExtName=<%=strImageExtName%>";
//You should know the type of the image you are
//downloading because it?? downloading as a stream
//and Dynamic Web TWAIN needs the correct type
//in order to process the stream correctly
DWObject.HTTPDownloadEx(strHTTPServer, downloadsource,<%=strImageFileType %>);
Is this page helpful?

Leave a Reply

Your email address will not be published.