File Uploader

The FileUploader is an independent module that handles the uploading of files. It's released with Dynamic Web TWAIN version 14.0. Once an upload job starts, it no longer relies on Dynamic Web TWAIN which means the upload carries on even after you have closed the browser. The files/data to upload is stored temporarily on the local disk (typically in the path C:\Windows\SysWOW64\Dynamsoft\DynamsoftService\upload) before they're uploaded and will be purged as soon as they're uploaded or when the Dynamsoft Service restarts.

Methods
Init() CreateJob() Run()
Cancel() CancelAllUpload() GenerateURLForUploadData()
Properties
ServerUrl HttpHeader SourceValue
FormField
Events
OnUploadTransferPercentage OnRunSuccess OnRunFailure

Code example

The following code example demonstrates how to use the APIs above to perform uploading.

var fileUploaderManager;

function onInitSuccess(objFileUploader) {
    fileUploaderManager = objFileUploader;
}

function onInitFailure(errorCode, errorString) {
    alert('Init failed: ' + errorString);
};

Dynamsoft.FileUploader.Init("", onInitSuccess, onInitFailure);

function UploadFile() {
    var job = fileUploaderManager.CreateJob();
    job.ServerUrl ='http://yourserver/youractionpage.aspx';
    job.FileName = "sample.jpg";
    job.ImageType = EnumDWT_ImageType.IT_JPG;
    DWObject.GenerateURLForUploadData([0],EnumDWT_ImageType.IT_JPG, function(resultURL, newIndices, enumImageType) {
        job.SourceValue.Add(resultURL, "sample.jpg");
        job.FormField.Add('customField', 'FormFieldValue');
        job.OnUploadTransferPercentage = FileUpload_OnUploadTransferPercentage;
        job.OnRunSuccess = FileUpload_OnRunSuccess;
        job.OnRunFailure = FileUpload_OnRunFailure;
        fileUploaderManager.Run(job);
    }, function(errorCode, errorString, strHTTPPostResponseString, newIndices, enumImageType) {
        console.log(errorString);
    });
}

function FileUpload_OnUploadTransferPercentage(job, sPercentage) {
    console.log(sPercentage);
}

function FileUpload_OnRunFailure(job, errorCode, errorString) {
    alert(errorString);
}

function FileUpload_OnRunSuccess(job) {
    alert(' upload completed! ');
}

Methods

Init()
This is a global API that initiates the FileUploader module.
Syntax Dynamsoft.FileUploader.Init(remoteFile, asyncSuccessFunc, asyncFailureFunc);
Parameters string remoteFile: an URL that specifies the path of the FileUploader library on the server. In v14.0, the library is installed together with the core|scan module of Dynamic Web TWAIN, so you can set this parameter to be an empty string.
OnSuccess function asyncSuccessFunc: callback function triggered when initiation succeeds, this function has a primitive parameter which refers to an UploadManager object that has just been initiated.
OnFailure function asyncFailureFunc: callback function triggered when initiation fails. When the FileUploader library (.dll) is not installed or the installed file is a different version, Init will try to download and install the library from the path specified by remoteFile. If that fails, this callback function is triggered.
Please refer to the function prototype OnSuccess or OnFailure.
Example
var dsUploadManager; 
Dynamsoft.FileUploader.Init('', function(obj) { 
    dsUploadManager = obj;
}, function(){});
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v14.0
CreateJob()
Create a upload job.
Syntax .CreateJob();
Parameters None
Return value Object A job object. For instance:
{HttpHeader: {}, documents: Array(0), Version: "1.0", HttpVersion: "1.1", ServerUrl: "", …}
Example
var job = dsUploadManager.CreateJob();
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v14.0
Run()
Starts to execute an upload job.
Syntax .Run(obj);
Parameters Object obj: A job object.
Return value boolean
Example
var job = dsUploadManager.CreateJob();
dsUploadManager.Run(job);
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v14.0
Cancel()
Cancels a upload job after it has started and before it completes. Normally this is done in the event OnUploadTransferPercentage.
Syntax .Cancel(obj);
Parameters Object obj: A job object.
Return value boolean
Example
var job = dsUploadManager.CreateJob();
job.OnUploadTransferPercentage= FileUpload_OnUploadTransferPercentage;
dsUploadManager.Run(job);
function FileUpload_OnUploadTransferPercentage(job, iPercentage)
{    
    console.log('job cancelled!')
    dsUploadManager.Cancel(job);
}
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v14.0
CancelAllUpload()
Cancels all upload jobs.
Syntax .CancelAllUpload();
Parameters None
Return value boolean
Example
dsUploadManager.CancelAllUpload();
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v14.0
GenerateURLForUploadData()
Generates a URL that will be used by the upload module to fetch the file/data to upload.
Syntax DWObject.GenerateURLForUploadData(indices, enumImageType, asyncSuccessFunc, asyncFailureFunc);
Parameters Number[] indices: the indices of the images in the buffer. The index is 0-based.
EnumDWT_ImageType enumImageType: the format in which you'd like the images to be uploaded.
OnSuccess function asyncSuccessFunc:callback function triggered when the operation succeeds. This function will return the result URL.
OnFailure function asyncFailureFunc: callback function triggered when the operation fails.
Please refer to the function prototype OnSuccess or OnFailure.
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){});
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v14.0

Properties

ServerUrl
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';
Type string
Accessors Get Set
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v14.0
HttpHeader
Specifies headers in the the HTTP Post Request of the upload job. For example: job.HttpHeader["Content-Type"] = "text/plain";
Type Object
Accessors Set
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v14.0
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
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.
Type Object
Accessors Set
Usage notes Use the Add(string urltoFetchFileData, string fileName) method of the Object to add data for uploading, check out the sample code for more information.
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v14.0
FormField
Specifies extra fields to be uploaded in the same HTTP post.
Type Object
Accessors Set
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.
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v14.0

Events

OnUploadTransferPercentage
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.
Syntax .OnUploadTransferPercentage = function(){...};
Arguments
  • Object obj: A job object.
  • number sPercentage: The percentage of the completion of the job.
Example
job.OnUploadTransferPercentage = FileUpload_OnUploadTransferPercentage; 
function FileUpload_ OnUploadTransferPercentage (obj, sPercentage){
    console.log(sPercentage);
}
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v14.0
OnRunSuccess
The event is triggered when an upload job completes successfully.
Syntax .OnRunSuccess = function(){...};
Arguments
  • Object obj: A job object.
Example
job.OnRunSuccess = FileUpload_OnRunSuccess; 
function FileUpload_OnRunSuccess(obj) { 
    alert(' upload completed ');
}
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v14.0
OnRunFailure
The event is triggered when an upload job fails.
Syntax .OnRunFailure = function(){...};
Arguments
  • Object obj: A job object.
  • number errorCode: The error code.
  • string errorString: The error string.
Example
job.OnRunFailure = FileUpload_OnRunFailure; 
function FileUpload_OnRunFailure(obj, errorCode, errorString) { 
    alert(errorString); 
}
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v14.0

results matching ""

    No results matching ""

    results matching ""

      No results matching ""