HTTPUploadThroughPostEx()

Description

Uploads the image of a specified index in buffer to the HTTP server in a specified image format.

Syntax

.HTTPUploadThroughPostEx(hTTPServer, sImageIndex, actionPage, fileName, imageType, [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.
EnumDWT_ImageType (int) imageType: specifies the format.

 

Type Numeric Value
IT_BMP 0
IT_JPG 1
IT_TIF 2
IT_PNG 3
IT_PDF 4

[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

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 programmatically upload the current selected image as a JEPG file.

JavaScript:

function btnScan_onclick() 
{
    if (DWObject) {
        DWObject.SelectSourceByIndex(document.getElementById("source").selectedIndex);
        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;
    // Uploads the a specific image in buffer to the HTTP server in a specified image format asynchronously
    DWObject.HTTPUploadThroughPostEx(
      strHTTPServer, 
      DWObject.CurrentImageIndexInBuffer, 
      strActionPage, 
      "imageData.jpg", 
      EnumDWT_ImageType.IT_JPG, 
      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{
	}
%>