The last article taught you how to upload images to the server. This article will continue with how to upload images to FTP and databases.

The operation page

The HTTP upload method makes a standard HTTP publish request to an action page on the server. Request contains image data, image name, etc. In the action page, you can process the image data as needed. Technically, you can write action pages in any server-side language (such as C#, VB, PHP, Java).

Here’s an example from C# :

This action page retrieves the image data from the current HTTP request object and stores it as a local file on the server.

HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile uploadfile = files["RemoteFile"];
uploadfile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(".") + "/" + uploadfile.FileName);Copy the code

Note that RemoteFile is the default name/key for uploading image data. If necessary, you can use the attribute HttpFieldNameOfUploadedImage change it.

In PHP:

$fileTempName = $_FILES['RemoteFile'] ['tmp_name'];  
$fileSize = $_FILES['RemoteFile'] ['size'];
$fileName = $_FILES['RemoteFile'] ['name']; 
move_uploaded_file($fileTempName.$fileName);Copy the code

Uploaded to the FTP

In addition to the HTTP upload method, you can also use the FTP upload method to update images to an FTP Web server. The apis available are:

format methods
Any type FTPUploadDirectly( )
Supported images FTPUpload( )

FTPUploadEx( )
Multipage PDF FTPUploadAllAsPDF( )

FTPUploadAsMultiPagePDF( )
Multi-page TIFF FTPUploadAllAsMultiPageTIFF( )

FTPUploadAsMultiPageTIFF( )

Code snippet

DWObject.FTPUserName = 'test';
DWObject.FTPPort = 21;
DWObject.FTPPassword = 'test';
DWObject.FTPUploadAllAsPDF(
    '192.168.8.222'.'test.pdf',
    OnFtpUploadSuccess,
    OnFtpUploadFailure
);Copy the code

Upload the image to the database

Dynamic Web TWAIN does not directly save/upload images to the database. Instead, the image data is stored first on the action page, and the code in the action page determines where to store it.

If you are not sure how to upload image data to the server, see the previous article “Uploading and saving on server Disks.”

Different database systems may have different image data data types. We generally use BLOB or varbinary in MSSQL Server, Long raw or BLOB in Oracle, and BLOB in MySQL.

The following is an example of using C+ in MSSQL Server:

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);

// add code to connect to database
String SqlCmdText = "INSERT INTO tblImage (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();Copy the code

In this snippet, we get the file object from the current HTTP request and write the image data to a byte array. In the SQL statement, we will byte array as a System. Data. SqlDbType. Binary is passed to the database, and storing the Data in BL imgImageData field.

Upload an image with additional data

Sometimes we need to pass more information to the server. For example, document type, employee ID, document description, etc. Since there is no way to pass attachment data in the HTTP upload method, we need to use a method called SetHTTPFormField.

SetHTTPFormField(String sFieldName, String sFieldValue)Copy the code
  • String sFieldName: Specifies the name of the field in the Web form
  • String sFieldValue: Specifies the value of this field in the Web form

We need to use this method before uploading. Such as:

DWObject.ClearAllHTTPFormField(); // Clear all fields first
DWObject.SetHTTPFormField("EmployeeID"."2012000054");
DWObject.SetHTTPFormField("DocumentType"."Invoice");
DWObject.SetHTTPFormField("DocumentDesc"."This is an invoice from ...");Copy the code

In the action page, you can retrieve data from the request object by field name. Such as:

String EmployeeID = HttpContext.Current.Request.Form["EmployeeID"];Copy the code

This is the end of the tutorial on how to upload images to a Web server using Dynamic Web TWAIN. In the next article, we will share how to download images from the Web

Dynamic Web TWAIN Latest edition download for free >>>