Dev Center
Table of contents

Dynamsoft.FileUploader

Methods

     
Init() CreateJob() Run()
Cancel() CancelAllUpload() GenerateURLForUploadData()

Properties

     
ServerUrl HttpHeader SourceValue
FormField    

Properties

     
OnUploadTransferPercentage OnRunSuccess OnRunFailure

Init

Syntax

/**
 * Initialize and create a FileUploader instance.
 * @param URL Specify a path to retrieve the FileUploader library.
 * @param successCallback A callback function that is executed if the request succeeds.
 * @param failureCallback A callback function that is executed if the request fails.
 * @argument uploadManager A FileUploader instance.
 * @argument errorCode The error code.
 * @argument errorString The error string.
 */
Init(
    URL: string,
    successCallback: (
        uploadManager: UploadManager
    ) => void,
    failureCallback: (
        errorCode: number,
        errorString: string
    ) => void
): void;

Availability

ActiveX H5(Windows) H5(macOS/TWAIN) H5(macOS/ICA) H5(Linux) WASM
not supported v17.2+ v17.2+ v17.2+ v17.2+ supported

Usage notes

The FileUploader library is installed with Dynamsoft Service by default, therefore, URL can be left empty “”.


CreateJob

Syntax

/**
 * Create an upload job.
 */
CreateJob(): Job;

interface Job {
    /**
     * Specify the block size (in bytes). By default, it's 10240.
     */
    BlockSize: number;
    /**
     * Specify the file name.
     */
    FileName: string;
    /**
     * Specify the fields in the HTTP Post Form.
     */
    FormField: FormField;
    /**
     * Specify custom HTTP Post request headers.
     * Example: job.HttpHeader["Content-Type"] = "text/plain";
     */
    HttpHeader: object;
    /**
     * Return the Http version.
     */
    readonly HttpVersion: string;
    /**
     * A callback triggered when the job succeeds.
     * @argument job Specify the job.
     * @argument errorCode The error code.
     * @argument errorString The error string.
     */
    OnRunFailure: (
        job: Job,
        errorCode: number,
        errorString: string
    ) => void;
    /**
     * A callback triggered when the job succeeds.
     * @argument job Specify the job.
     */
    OnRunSuccess: (job: Job) => void;
    /**
     * A callback triggered multiple times during the upload.
     * @argument job Specify the job.
     * @argument percentage Return the percentage.
     */
    OnUploadTransferPercentage: (
        job: Job,
        percentage: number
    ) => void;
    /**
     * Specify the URL of the script to receive the upload.
     */
    ServerUrl: string;
    /**
     * Specify the main content of the job, i.e. the file(s).
     */
    SourceValue: SourceValue;
    /**
     * Specify the number of threads (<=4) for the upload.
     */
    ThreadNum: number;
    /**
     * Return the version of the job.
     */
    readonly Version: number;
}
interface SourceValue {
    /**
     * Specify the block size. By default, it's 10240.
     * @param source A URL to specify the content of the file.
     * Normally it's generated by {GenerateURLForUploadData()}
     * @param name Specify the name of the file.
     * @param key Specify the key of the file in the request. This key can be used to retrieve the file content in server-side scripts.
     */
    Add: (
        source: string,
        name: string,
        key ? : string
    ) => void;
}
interface FormField {
    /**
     * Specify the block size. By default, it's 10240.
     * @param key Specify the key of the field.
     * @param value Sepcify the value of the field.
     */
    Add: (
        key: string,
        value: string
    ) => void;
}

Availability

ActiveX H5(Windows) H5(macOS/TWAIN) H5(macOS/ICA) H5(Linux) WASM
not supported v17.2+ v17.2+ v17.2+ v17.2+ supported

Run

Syntax

/**
 * Start uploading (processing the specified job).
 * @param job Specify the job.
 */
Run(job: Job): boolean;

Availability

ActiveX H5(Windows) H5(macOS/TWAIN) H5(macOS/ICA) H5(Linux) WASM
not supported v17.2+ v17.2+ v17.2+ v17.2+ supported

Cancel

Syntax

/**
 * Cancel a job.
 * @param job Specify the job.
 */
Cancel(job: Job): boolean;

Availability

ActiveX H5(Windows) H5(macOS/TWAIN) H5(macOS/ICA) H5(Linux) WASM
not supported v17.2+ v17.2+ v17.2+ v17.2+ supported

CancelAllupload

Syntax

/**
 * Cancel all jobs.
 */
CancelAllUpload(): boolean;

Availability

ActiveX H5(Windows) H5(macOS/TWAIN) H5(macOS/ICA) H5(Linux) WASM
not supported v17.2+ v17.2+ v17.2+ v17.2+ supported

Usage notes

Cancel() or CancleAllUpload() should be called in the event OnUploadTransferPercentage.

Example

var dsUploadManager;
Dynamsoft.FileUploader.Init(
  "",
  function (obj) {
    dsUploadManager = obj;
    var job = dsUploadManager.CreateJob();
    job.OnUploadTransferPercentage = FileUpload_OnUploadTransferPercentage;
    dsUploadManager.Run(job);

    function FileUpload_OnUploadTransferPercentage(job, iPercentage) {
      console.log("job cancelled!");
      dsUploadManager.Cancel(job);
    }
  },
  function () {}
);

GenerateURLForUploadData

Syntax

/**
 * Generates a URL that will be used by the upload module to fetch the file/data to upload.
 * @param Number[] The indices of the images in the buffer. The index is 0-based.
 * @param EnumDWT_ImageType The format in which you'd like the images to be uploaded.
 * @param successCallback A callback function triggered when the operation succeeds. This function will return the result URL.
 * @param failureCallback A callback function triggered when the operation fails.
 * @argument resultURL The result URL.
 * @argument errorCode The error code.
 * @argument errorString The error string.

 */
GenerateURLForUploadData(
    Number[]: array,
    EnumDWT_ImageType: Dynamsoft.DWT.EnumDWT_ImageType | number,
    successCallback: (
        resultURL: resultURL
    ) => void,
    failureCallback: (
        errorCode: number,
        errorString: string
    ) => void
): void;

Availability

ActiveX H5(Windows) H5(macOS/TWAIN) H5(macOS/ICA) H5(Linux) WASM
not supported v17.2+ v17.2+ v17.2+ v17.2+ supported

Example

Dynamsoft.FileUploader.Init(
  "",
  function (obj) {
    dsUploadManager = obj;
  },
  function () {}
);
DWObject.GenerateURLForUploadData(
  [0, 1],
  EnumDWT_ImageType.IT_PDF,
  function (resultURL, newIndices, enumImageType) {
    var serverUrl = "https://yoursite/yourserverurl.aspx";
    var jobtemp = dsUploadManager.CreateJob();
    jobtemp.ServerUrl = serverUrl;
    jobtemp.SourceValue.Add(resultURL, "uploadedFile.pdf");
    dsUploadManager.Run(jobtemp);
  },
  function (
    erroCode,
    errorString,
    httpResponseString,
    newIndices,
    enumImageType
  ) {}
);

ServerUrl

Syntax

/**
 * Specifies the target of the HTTP Post Request of the upload job. This typically is a file on the server. For example: job.ServerUrl = 'http://www.dynamsoft.com/ScanAndUpload/Actions/SaveToFile.aspx';
 */
ServerUrl: string;

Availability

ActiveX H5(Windows) H5(macOS/TWAIN) H5(macOS/ICA) H5(Linux) WASM
not supported v17.2+ v17.2+ v17.2+ v17.2+ supported

HttpHeader

Syntax

/**
 * Specifies headers in the the HTTP Post Request of the upload job. For example: job.HttpHeader["Content-Type"] = "text/plain";
 */
HttpHeader: object;

Availability

ActiveX H5(Windows) H5(macOS/TWAIN) H5(macOS/ICA) H5(Linux) WASM
not supported v17.2+ v17.2+ v17.2+ v17.2+ supported

Usage notes

By default, HttpHeader is an empty object. If left as it is, default headers are used. Otherwise, the headers set by this property will be added to the HTTP Post Request or replace existing ones with the same names.


SourceValue

Syntax

/**
 * Specifies the files to be uploaded and the name for it. The files are specified by URLs which can be created with the method GenerateURLForUploadData. This object has a method Add to add file to the job.
 */
SourceValue: object;

Availability

ActiveX H5(Windows) H5(macOS/TWAIN) H5(macOS/ICA) H5(Linux) WASM
not supported v17.2+ v17.2+ v17.2+ v17.2+ supported

Usage notes

Use the Add(string urltoFetchFileData, string fileName) method of the Object to add data for uploading.

Example

job.SourceValue.Add(url, fileName);

FormField

Syntax

/**
 * Specifies extra fields to be uploaded in the same HTTP post.
 */
FormField: object;

Availability

ActiveX H5(Windows) H5(macOS/TWAIN) H5(macOS/ICA) H5(Linux) WASM
not supported v17.2+ v17.2+ v17.2+ v17.2+ supported

Usage notes

Use the Add(string fieldName, string fieldValue) method of the Object to add fields for uploading, check out the sample code for more information.

Example

job.FormField.Add("customField", "FormFieldValue");

OnUploadTransferPercentage

Syntax

/**
 * The event is triggered during the execution of an upload job. It has a parameter which specifies the percentage of the completion of the job.
 * @argument obj  A job object.
 * @argument sPercentage The percentage of the completion of the job.
 */
.OnUploadTransferPercentage = function(obj: Object , sPercentage: number){};

Availability

ActiveX H5(Windows) H5(macOS/TWAIN) H5(macOS/ICA) H5(Linux) WASM
not supported v17.2+ v17.2+ v17.2+ v17.2+ supported

Example

job.OnUploadTransferPercentage = FileUpload_OnUploadTransferPercentage;
function FileUpload_ OnUploadTransferPercentage (obj, sPercentage){
    console.log(sPercentage);
}

OnRunSuccess

Syntax

/**
 * The event is triggered when an upload job completes successfully.
 * @argument obj  A job object.
 */
.OnRunSuccess = function(obj: Object){};

Availability

ActiveX H5(Windows) H5(macOS/TWAIN) H5(macOS/ICA) H5(Linux) WASM
not supported v17.2+ v17.2+ v17.2+ v17.2+ supported

Example

job.OnRunSuccess = FileUpload_OnRunSuccess;
function FileUpload_OnRunSuccess(obj) {
  alert(" upload completed ");
}


OnRunFailure

Syntax

/**
 * The event is triggered when an upload job completes successfully.
 * @argument obj  A job object.
 * @argument errorCode The error code.
 * @argument errorString The error string.
 */
.OnRunFailure = function(
obj: Object
errorCode: number,
errorString: string
){};

Availability

ActiveX H5(Windows) H5(macOS/TWAIN) H5(macOS/ICA) H5(Linux) WASM
not supported v17.2+ v17.2+ v17.2+ v17.2+ supported

Example

job.OnRunFailure = FileUpload_OnRunFailure;
function FileUpload_OnRunFailure(obj, errorCode, errorString) {
  alert(errorString);
}

Is this page helpful?

YesYes NoNo

In this article:

latest version

  • Latest Version
  • Version 17.1.1
  • Version 17.0
  • Version 16.2
  • Version 16.1.1
Change +
© 2003–2022 Dynamsoft. All rights reserved.
Privacy Statement / Site Map / Home / Purchase / Support