Upload images/documents to the web server

We can call our built-in HTTP upload method to upload the captured image(s) or document(s) asynchronously.

Format Method
PNG, JPEG, PDF uploadAsync() Swift, Objective-C

Below is the code snippet for your reference:

NSMutableArray *IndexArr = [[NSMutableArray alloc] init];
for(NSInteger i = 0;i < [self.dcsView.buffer count];i++){
    NSNumber *num = [NSNumber numberWithInteger:i];
    [IndexArr addObject:num];
NSArray *UploadIndices = [IndexArr copy];
DcsHttpUploadConfig *uploadConf = [[DcsHttpUploadConfig alloc]init];
uploadConf.url = @"<your upload url>"; //Sets the url of the action page. Please change it to your own.
uploadConf.name = @"RemoteFile";  //Sets the name/key for the uploaded image data. The default value is 'name'.
uploadConf.uploadMethod = DUME_POST; //Sets the upload method to POST.
uploadConf.dataFormat = DDFE_BINARY; //Sets the data format to binary.
uploadConf.filePrefix = @"Dynamsoft";      //Sets the prefix of the file name. The default value is nil.
DcsPDFEncodeParameter *PdfEncode = [[DcsPDFEncodeParameter alloc]init];

NSData *response = [[NSData alloc]init];
[dcsView.io uploadAsync:UploadIndices uploadConfig:uploadConf encodeParameter:PdfEncode
     successCallback:^(NSData* data){NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];NSLog(@"UploadAsync succeeds! data: %@", str);}
                   failureCallback:^(id userData, DcsException *exception){NSLog(@"UploadAsync fails with exception: %@,%@", exception.name, exception.reason);NSLog(@"UploadAsync fail with userdata: %@", userData);}
                   progressUpdateCallback:^BOOL(NSInteger progress){NSLog(@"UploadAsync on going with %ld percent done", progress);return YES;}];

        var config = DcsHttpUploadConfig()
        var pdf = DcsPDFEncodeParameter()
        config.url = "<your upload url>" //specifies the url of the action page. Please change the url to your own
        config.name = "Remotefile" //specifies the name/key of the uploaded file. The default value is "name"
        config.uploadMethod = DUME_POST //specifies the upload method as POST
        config.dataFormat = DDFE_BASE64 //specifies the data format as BASE64 String. Byte is also supported.
        config.filePrefix = "Dynamsoft" //specifies the prefix of the file name. The default value is nil
        var indexArr = Array<Int>()
        for index in 0...dcsView.buffer.count()-1{
        dcsView.io.uploadAsync(indexArr, uploadConfig: config, encodeParameter: pdf, successCallback: { (receive: Data) in
            print("successful.",String(data: receive!, encoding: .utf8))
        }, failureCallback: { (userData: Any, exp: DcsException!) in
        }) { (progress: Int) -> Bool in
            return true

With the above code, all images in DcsBuffer will be sent to the web server as one multi-page PDF file.

The parameter uploadConfig contains the configuration for HTTP server and parameter encodeParameter is used to define what format the image(s) will be encoded to.

        DcsPDFEncodeParameter *PdfEncode = [[DcsPDFEncodeParameter alloc]init];    // Encoded as PDF
        DcsPNGEncodeParameter *PngEncode = [[DcsPNGEncodeParameter alloc]init];    // Encoded as PNG
        DcsJPEGEncodeParameter *JpegEncode = [[DcsJPEGEncodeParameter alloc]init]; // Encoded as JPEG
        var png=DcsPNGEncodeParameter() // Encoded as PNG
        var pdf=DcsPDFEncodeParameter() // Encoded as PDF
        var jpg=DcsJPEGEncodeParameter() // Encoded as JPEG

Action page

The HTTP upload method makes a standard HTTP post request to the action page on the server. The request contains the image data, image name, etc. In the action page, you can process the image data according to your requirements. Technically you can write the action page in any server-side language (C#, VB, PHP, Java, etc...).

Here is an example in C#:

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

<%@ Page Language="C#" %><%
String strImageName; 
HttpFileCollection files = HttpContext.Current.Request.Files;
int iCount = HttpContext.Current.Request.Files.Count; 
for(int i=0;i< iCount; i++)
HttpPostedFile uploadfile = files[i];
strImageName = uploadfile.FileName;

Response.Write("filename: ");
uploadfile.SaveAs(Server.MapPath(".") + "\\UploadedImages\\" + strImageName); 
Response.Write("   file Count:"); 
for (int j = 0; j < files.AllKeys.Length; j++) 
   Response.Write("   name: " + Server.HtmlEncode(files.AllKeys[j]) + " ");
Response.Write ("failed");

Note: Please note that 'name' is the default name/key for the uploaded image data. In the example code, we set it to "RemoteFile".

We use timestamp (milliseconds since January 1, 1970) to name the file by default. For example, 1477658922059.png, the exact time is 2016-10-28 20:48:42. If you have set the property filePrefix, for example, in the above code, we set it to "Dynamsoft", then the file name should look like Dynamsoft1477658922059.png.

Uploading images with extra data

If you are not sure how to upload the image data to the server, please refer to the previous topic "Upload images/documents to the web server".

Sometimes we need to pass more information to the server. For example, document type, employee ID, document description, etc. Since we don't have any options to pass extra data in the HTTP upload method, we need to use a property called formField.

Set this property before the HTTP Upload method. Here is an example:

NSDictionary *dictFormField = @{@"EmployeeID":@123,@"DocumentType":@"Invoice"};
uploadConf.formField = dictFormField;

As you can see, the value is expressed in a format like {@fieldName: @fieldValue, @fieldName: @fieldValue,...}

  • fieldName: specifies the name of a text field in the form.
  • fieldValue: specifies the value of a text field in the form.

In the action page, you can retrieve the data from the request object by the field names. For example:

String EmployeeID = HttpContext.Current.Request.Form['EmployeeID'];

Is this page helpful?

Leave a Reply

Your email address will not be published.