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

Below is the code snippet for your reference:

DcsHttpUploadConfig config = new DcsHttpUploadConfig();
        DcsEncodeParameter pdf = new 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.DataFormat = DcsHttpUploadConfig.DDFE_BASE64; //specifies the data format as BASE64 String. Byte is also supported.
        config.UploadMethod = DcsHttpUploadConfig.DUME_POST; //specifies the upload method as POST.
        config.filePrefix = "Dynamsoft"; //specifies the prefix of the file name. The default value is null.
        HashMap<String, String> map = new HashMap<>();
        config.FormField = map;
        int[] array = dcsView.getImageGalleryView().getSelectedIndices();
        dcsView.getIO().uploadAsync(array, config, pdf, new IUpload() {
            @Override
            public boolean onUploadProgress(int progress) {
               	Log.d("upload progress", "" +progress);
                return true;
            }

            @Override
            public void onUploadSuccess(byte[] data) {
                if (data != null) {
                    try {
                        Log.d("upload success", "succeed : " + new String(data, "UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onUploadFailure( DcsException exp) {
                Log.d("upload error", "" + exp.getMessage());
            }
        });
    }

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.

DcsEncodeParameter png= new DcsPNGEncodeParameter() // Encoded as PNG
DcsEncodeParameter pdf= new DcsPDFEncodeParameter() // Encoded as PDF
DcsEncodeParameter jpg= new 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.

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:

 HashMap<String, String> map = new HashMap<>();
 map.put("EmployeeID", "123");
 map.put("DocumentType",  "invoice");
 config.FormField = map;

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.