uploadDocument Method


Uploads the specified data to the cache.


JavaScript Syntax
uploadDocument = function( 

TypeScript Syntax
static uploadDocument( 
   uri: string, 
   data: ArrayBuffer, 
   offset: number, 
   length: number 
): JQueryPromise<any>; 



The URI to upload to, which came from the result of BeginUpload.


The binary data, of JavaScript type ArrayBuffer, to upload.


The position within data to get the chunk to upload.


The length of the chunk from data to upload, relative to offset.

Return Value

A voidPromise object that may resolve successfully.


After BeginUpload has resolved and returned a URI, this method can be used to upload chunks of the document. BeginUpload, UploadDocument, and AbortUploadDocument are used together to get the same functionality as UploadFile, giving more control over the upload process. This method is often called in a loop to upload chunks of data to the cache over and over until the entire document has been uploaded. UploadDocument gives the developer control over when and how chunks are uploaded.

UploadDocument should not be called multiple times in a row without waiting for each call's Promise to be resolved, or the uploaded LEADDocument may be corrupt.

This method does not receive a DocumentUploadProgress object in the progress callback of the Promise.

When uploading is finished, EndUpload can optionally be called to inform the factory that the uploading process has finished. EndUpload is optional and the factory will not perform any actions during this call. It can be used as a trigger to other parts of the system that the document has finished uploading.

Unlike with UploadFile, the Promise object returned by this method does not have an Abort method. Instead, AbortUploadDocument must be called with the same uploadUri being used to upload the document. This will not necessarily cause the Promise object from this method to fail immediately, but continued calls to UploadDocument will.

Note that the browser must support the FileReader API in order to manipulate filesystem data on the client. If the FileReader API is not supported, an error will be thrown and IsBrowserError will return true.

Refer to Uploading Using LEADTOOLS Document Library for more information on how to use this method.


This example will download, upload, and load a document from the service to show that it is given a new cache entry.

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
// Load the initial document 
var url = "https://demo.leadtools.com/images/pdf/leadtools.pdf"; 
log("Loading initial document..."); 
lt.Document.DocumentFactory.loadFromUri(url, new lt.Document.LoadDocumentOptions()) 
   .done(function (doc) { 
      log("Document loaded and has cache id: " + doc.documentId); 
var overallData, dataSize, location, length, remaining; 
function afterLoadDocument(doc) { 
   overallData = []; 
   dataSize = 1024 * 1024; 
   location = 0; 
   length = doc.values.fileLength; 
   remaining = length; 
   // Download the document data piece by piece. 
   log("Now downloading document data (length " + length + ")"); 
   // Start downloading 
   lt.Document.DocumentFactory.downloadDocument(doc.documentId, null, location, dataSize) 
function downloadedPiece(data) { 
   log("Received data of length " + data.length); 
   overallData = overallData.concat(data); 
   location += data.length; 
   remaining = length - data.length; 
   // Done downloading 
   if (remaining <= 0) { 
      log("All data downloaded. Now uploading..."); 
   else { 
      // Download it 
      lt.Document.DocumentFactory.downloadDocument(doc.documentId, null, location, dataSize) 
function upload(overallData) { 
   // get an arraybuffer from the byte array 
   var uploadLength = overallData.length; 
   var bytes = new Uint8Array(uploadLength); 
   overallData.forEach(function (val, i) { 
      bytes[i] = val; 
   var arrayBuffer = bytes.buffer; 
   // Done with downloading file. Now upload. 
   lt.Document.DocumentFactory.beginUpload().done(function (uri) { 
      log("Uploading to location " + uri); 
      var filePosition = 0; 
      // if we aren't done, send the next request. 
      var uploadedPiece = function (userData) { 
         filePosition += dataSize; 
         if (filePosition < uploadLength) { 
            // Keep uploading 
            lt.Document.DocumentFactory.uploadDocument(uri, arrayBuffer, filePosition, dataSize) 
         else { 
            lt.Document.DocumentFactory.endUpload().done(function () { 
               // Fully uploaded 
               log("Document uploaded successfully"); 
               lt.Document.DocumentFactory.loadFromUri(uri, null) 
                  .done(function (doc) { 
                     log("Document was loaded succesfully from " + uri); 
                     log("Document has new id " + doc.documentId) 
      // Upload next part 
      lt.Document.DocumentFactory.uploadDocument(uri, arrayBuffer, filePosition, dataSize) 


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