HTTPUploadThroughPost()

Description

Uploads the image of a specified index in Dynamic Web TWAIN viewer to the HTTP server.

Syntax

.HTTPUploadThroughPost(hTTPServer, sImageIndex, actionPage, fileName, [optionalAsyncSuccessFunc, optionalAsyncFailureFunc]);

Parameters

<string> hTTPServer: specifies the name of HTTP server. For example: "www.dynamsoft.com", "localhost", "127.0.0.1".
<short> sImageIndex: specifies the index of image in buffer. The index is 0-based.
<string> actionPage:specifies the relative path for the action page. The action page will receive and process the uploaded image stream. See code snippet.
<string> fileName: specifies the file name.

[HTML5 edition only]The following two parameters are optional. If either one exists or both exist, the method is asynchronous, otherwise it's synchronous.

<OnHttpUploadSuccess function> optional optionalAsyncSuccessFunc: callback function triggered when the file is uploaded successfully.
<OnHttpUploadFailure function> optional optionalAsyncFailureFunc: callback function triggered when the file failed to be uploaded.

Please refer to the function prototype OnHttpUploadSuccess or OnHttpUploadFailure.

Return value

Only valid when used synchronously.

bool

true indicates success. false indicates failure.

When false is returned, check ErrorCode or ErrorString for error information.

Remarks

Dynamic Web TWAIN processes the image format according to the extension of fileName.

If you want to use this method to upload / download files through HTTPS, please don't forget to set IfSSL to true and set the correct HTTPPort.

Example

The sample shows how to upload the current selected image as a JPEG / PNG / BMP file.

JavaScript:

function btnScan_onclick() 
{
    if (DWObject) {
        DWObject.SelectSource();
        DWObject.OpenSource();
        DWObject.IfDisableSourceAfterAcquire = true;
        DWObject.AcquireImage();
    }
}
// OnHttpUploadSuccess and OnHttpUploadFailure are callback functions.
function OnHttpUploadSuccess() {
    console.log('successful');
}
function OnHttpUploadFailure(errorCode, errorString, sHttpResponse) {
    alert(errorString + sHttpResponse);
}
function btnUpload_onclick() 
{
    var strHTTPServer = location.hostname; //The name of the HTTP server. 
    var CurrentPathName = unescape(location.pathname);
    var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);
    var strActionPage = CurrentPath + "SaveToFile.aspx";
    DWObject.IfSSL = false; // Set whether SSL is used
    DWObject.HTTPPort = location.port == "" ? 80 : location.port;
    // Upload the image of a specified index in Dynamic Web TWAIN viewer to the HTTP server asynchronously
    if (document.getElementById("JPEG").checked) {
    	DWObject.HTTPUploadThroughPost(
    	  strHTTPServer, 
    	  DWObject.CurrentImageIndexInBuffer,      		 
    	  strActionPage,     		 
    	  "imageData.jpg",     		 
    	  OnHttpUploadSuccess,     		 
    	  OnHttpUploadFailure
    	);
    }
    if (document.getElementById("PNG").checked) {
    	DWObject.HTTPUploadThroughPost(
    	  strHTTPServer,     		
    	  DWObject.CurrentImageIndexInBuffer,     		
    	  strActionPage,    		
    	  "imageData.png",     		
    	  OnHttpUploadSuccess,    		
    	  OnHttpUploadFailure
    	);
    }
    if (document.getElementById("BMP").checked) {
    	DWObject.HTTPUploadThroughPost(
    	  strHTTPServer,     		
    	  DWObject.CurrentImageIndexInBuffer,     		
    	  strActionPage,    		
    	  "imageData.bmp",    		
    	  OnHttpUploadSuccess,    		
    	  OnHttpUploadFailure
    	);
    }
}

SaveToFile.aspx:

<%
    try{
        String strImageName;
        HttpFileCollection files = HttpContext.Current.Request.Files;
        HttpPostedFile uploadfile = files["RemoteFile"];
        strImageName = uploadfile.FileName;
        uploadfile.SaveAs(Server.MapPath(".") + "\\UploadedImages\\" + strImageName);
    }
    catch{
	}
%>
Is this page helpful?

Leave a Reply

Your email address will not be published.