Uploading Using the Document Library

Summary

The Document library supports uploading documents from the user drive to the cache system. This support comes handy when the cache is hosted on a separate machine not accessible to the user. For example, when the Documents class is hosted in a web service with a JavaScript or rich client application. Such as the HTML5/JavaScript version of the LEADTOOLS Document Viewer using the LEADTOOLS Document Web Service.

There are two ways to upload a document to the cache system. The first, more "automatic" method is to provide the JavaScript File object to UploadFile, which returns a jQuery Promise for a string uri for the cached Document. The second way involves calling BeginUpload or BeginUploadDocument to first retrieve the uri, then successive calls to UploadDocument to upload chunks of a JavaScript File object manually. In both ways of uploading, the uri can be used with LoadFromUri to download the Document data.

When uploading is finished, EndUpload must be called to inform the factory that the uploading process has finished.

Refer to UploadFile or UploadDocument for examples.

Enable Streaming

During the upload process, the DocumentFactory will save each chunk into the cache during UploadDocument. Therefore, the latest document data is available to all users of the system. If the system encounters an error and the process restarts, the upload operation can be restarted and uploading continues. This also ensures minimum amount of memory is needed when uploading large documents (only chunk size is need at any time). This the default behavior when UploadDocumentOptions.EnableStreaming is false (the default value).

If EnableStreaming is set to true, then the DocumentFactory will not save the chunk data into the cache during UploadDocument. Instead it will create an internal stream in memory and append the chunks into it as they arrive. When EndUpload is called, the factory will then store all the data at once from the stream into the cache. This may speed up uploading of document on the expense of more memory being used (the whole document data will be in memory) and uploading operations cannot be restarted if the system encounters an error and the process restarts. Therefore, it is recommended not to set EnableStreaming to true unless the system is designed for single process or single user operations.

Post Upload Operations

Post upload operations can be perform on a document after it has been uploaded to the cache and before it is first loaded. These operations and their optional values can be set in the UploadDocumentOptions.PostUploadOperations dictionary.

The operations are performed when EndUpload is called.

The current version of LEADTOOLS contains support for the following post upload operations:

Auto-Linearize PDF

Linearized PDF are documents that are optimized for fast web viewing. The LEADTOOLS JavaScript Document Viewer when using client-side rendering can take advantage of linearized PDF to start viewing the PDF file before all its content is downloaded.

Uploaded PDF files can be checked for linearization and converted upon upload using the following:

javascript 
// Automatically linearize (optimize for fast web viewing) PDF files that are greater than 1 MBytes in size. 
var pdfData = ...; // PDF data to upload as a byte array 
var pdfPassword = null; // If the PDF is encrypted, set its password here 
var uploadDocumentOptions = new lt.Document.UploadDocumentOptions(); 
var minimumLengthInBytes = 1024 * 1024; 
uploadDocumentOptions.postUploadOperations[lt.Document.LEADDocument.postUpload_LinearizePdfMinimumLength] = minimumLengthInBytes.toString(); 
// The factory will not perform this operation unless we set the correct mime type: 
uploadDocumentOptions.mimeType = "application/pdf"; 
uploadDocumentOptions.password = pdfPassword; 
// Now upload 
var uploadDocumentUri; 
lt.Document.DocumentFactory.beginUploadDocument(uploadDocumentOptions) 
   .done(function (documentUri) { 
      uploadDocumentUri = documentUri; 
      lt.Document.DocumentFactory.uploadDocument(documentUri, pdfData, 0, pdfData.length); 
   }) 
   .done(function () { 
      lt.Document.DocumentFactory.endUpload(uploadDocumentUri); 
   }); 

The factory will perform the following actions on endUpload:

  • Determine if the total upload size is greater than minimumLengthInBytes.
  • If so, will call the toolkit PDFFile.IsLinearized to check if the PDF is not already optimized, and if not, calls PDFFile.Linearize.

If the PDF is encrypted then a password is required to linearize it. This can be passed in UploadDocumentOptions.Password as shown above.

The metadata of LEADDocument can be examined to determine if this is a PDF document with linearized data using the following:

javascript 
lt.Document.DocumentFactory.loadFromCache(documentId) 
   .done(function (leadDocument) { 
      var isLinearized = false; 
      // Check if the metadata contains the key 
      var isLinearizedValue = leadDocument.metadata[lt.Document.LEADDocument.metadataKey_IsLinearized]; 
      if (isLinearizedValue) { 
         // Yes 
         isLinearized = (isLinearizedValue === "true"); 
      } 
      if (isLinearized) { 
         // Perform additional actions 
      } 
   }); 

Help Version 20.0.2019.4.12
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2019 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS HTML5 JavaScript