Dynamic Web TWAIN: How to upload images directly to Database?


Upload an image into the server's database using Dynamic Web TWAIN.


Here are some methods to achieve this.


1.Add an action page to save the image data to the database.

For example, we can use C# or VB.NET:

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

 iFileLength = uploadfile.ContentLength;
 Byte[] inputBuffer = new Byte[iFileLength];
 System.IO.Stream inputStream;
 inputStream = uploadfile.InputStream;
 inputStream.Read(inputBuffer, 0, iFileLength);
 String sql_insertData = "INSERT INTO " + <em>tableName</em> + " (document_name, document_data) VALUES (@document_name, @document_data)";
 using (System.Data.SqlClient.SqlCommand sqlcmd_insertData = new System.Data.SqlClient.SqlCommand(sql_insertData, Connection))
  sqlcmd_insertData.Parameters.Add("@document_data", System.Data.SqlDbType.Binary, iFileLength).Value = inputBuffer;
  sqlcmd_insertData.Parameters.Add("@document_name", System.Data.SqlDbType.VarChar, 255).Value = strImageName;
catch (Exception) { } %&gt;

2.Add the upload method and the action page.

On the client side, use the above mentioned HTTPUploadXXX methods to upload the image. Put the action page SaveToDB.aspx as the destination of the image. For more info, please refer to Uploading the image(s) to a web server of the Developers' Guide

3.Set up SQL Server

To be noted, when uploading files into database, we also need to make sure the account we are using has the write permission on the database. Take the Internet Information Service(IIS) server for example, if the identity NetworkService is used, as can be checked from the following setting.

Then in Microsoft SQL Server, you need to make sure a server role with write permission, such as sysadmin, is checked.

Is this page helpful?

Leave a Reply

Your email address will not be published.