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];
        //Sets the url of the action page.
        uploadConf.url = @"";  
        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_BASE64; //Sets the data format to Base64.
        uploadConf.filePrefix = @"Dynamsoft";      //Sets the prefix of the file name. The default value is nil.
        DcsPDFEncodeParameter *PdfEncode = [[DcsPDFEncodeParameter alloc]init];

        NSData *response = [NSData alloc];    
        [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 percents done", progress);return YES;}];

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

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.

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

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.