uploadFile Method


Uploads a new document to the server.


JavaScript Syntax
uploadFile = function( 

TypeScript Syntax
static uploadFile( 
   file: File, 
   documentId: string 
): AbortableJqueryPromise; 



The JavaScript File object representing the file to upload.


Optional: The ID to be used with the loaded document.

Return Value

A Promise object that may resolve successfully to a special uri pointing to the location of the newly-created LEADDocument in the cache (verifiable with IsUploadDocumentUri). The returned Promise has a unique type of AbortableJQueryPromise that holds a Abort method.


When the value of documentId is null (the default), then the document factory will create a new unique ID using a GUID generator. If the value is not null, then it is assumed to be a user-defined ID and used as is. In either case, the value is set in the LEADDocument.DocumentId property of the newly created document.

User-defined IDs can be used when the system already have unique ID's associated with the documents to be viewer. The document factory will not check nor guarantee the uniqueness of these IDs.

The UploadFile method is an abstraction of three other methods used together to manage the upload of a resource - BeginUpload, UploadDocument, and AbortUploadDocument. UploadFile wraps together the functionality of these three methods, and returns a url to the uploaded resource when the Promise resolves. This url can be used to load the document with LoadFromUri.

The Promise for this method can receive progress events. Refer to DocumentUploadProgress for more information.

The Promise object that is returned immediately by calling UploadFile is of type AbortableJQueryPromise, an extension of the usual jQuery Promise object. This class adds an additional method, Abort, which has the same functionality as AbortUploadDocument but is not static and thus does not need the uri of the uploading file. If the upload is aborted, the fail callback to the Promise object will be called with all three parameters as null. See Promises for more information.

This method uses the JavaScript File object to obtain access to a file on the local machine. Please note that the browser must support the FileReader API in order to manipulate filesystem data on the local machine. If the FileReader API is not supported, an error will be thrown and IsBrowserError will return true.

Refer to Uploading Using the Document Library for detailed information on how to use this method and the various options used.


This example will upload a file from the local machine and then download it as a LEADTOOLS Document object ready to be used.

Start with the example from LEADDocument and replace all the code inside the example function (search for the "// TODO: add example code here" comment) with the following code:

JavaScript Example
// Add a file select 
var fileInput = document.createElement("input"); 
fileInput.type = "file"; 
var exampleButton = document.getElementById("exampleButton"); 
// Add an "upload" button 
var uploadButton = document.createElement("button"); 
uploadButton.type = "button"; 
uploadButton.innerHTML = "Click to Upload"; 
uploadButton.onclick = function () { 
   // Get the File object to upload 
   var file = fileInput.files[0]; 
   if (!file) { 
      alert("No file!"); 
   // Upload it 
      .done(function (uri) { 
         // Done, now load it 
         log("Finished uploading. Now loading..."); 
         var loadDocumentOptions = new lt.Document.LoadDocumentOptions(); 
         // Set the name 
         loadDocumentOptions.name = file.name; 
         lt.Document.DocumentFactory.loadFromUri(uri, loadDocumentOptions) 
            .done(function (doc) { 
               log("Document was loaded succesfully"); 
               log("Name is " + doc.name); 
               log("MIMEType is " + doc.mimeType); 
               log("Number of Pages is " + doc.pages.count); 


Target Platforms

Help Version 20.0.2018.8.29
Products | Support | Contact Us | Copyright Notices
© 1991-2018 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document Assembly