uploadFile Method

Summary

Uploads a new document to the server.

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

Parameters

file

The JavaScript File object representing the file to upload.

documentId

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 the Abort method.

Remarks

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 has unique IDs 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 - BeginUploadDocument, 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 it 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.

Example

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:

TypeScript
JavaScript
// Add a file select 
var fileInput = document.createElement("input"); 
fileInput.type = "file"; 
var exampleButton = document.getElementById("exampleButton"); 
exampleButton.parentNode.appendChild(fileInput); 
 
// Add an "upload" button 
var uploadButton = document.createElement("button"); 
uploadButton.type = "button"; 
uploadButton.innerHTML = "Click to Upload"; 
exampleButton.parentNode.appendChild(uploadButton); 
uploadButton.onclick = function () { 
 
   // Get the File object to upload 
   var file = fileInput.files[0]; 
   if (!file) { 
      alert("No file!"); 
      return; 
   } 
 
   // Upload it 
   log("Uploading..."); 
   lt.Document.DocumentFactory.uploadFile(file) 
      .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); 
            }) 
            .fail(showServiceError); 
      }) 
      .fail(showServiceError); 
} 
class DocumentFactory_UploadFileExample { 
    public constructor() { 
        lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/v200/LEADTOOLSEVAL.txt", "EVAL", null); 
    } 
 
    public run(buttonID: string) { 
        const exampleButton = document.getElementById(buttonID); 
 
        // Add a file select 
        const fileInput = document.createElement("input"); 
        fileInput.type = "file"; 
        exampleButton.parentNode.appendChild(fileInput); 
 
        // add an "upload" button 
        const uploadButton = document.createElement("button"); 
        uploadButton.type = "button"; 
        uploadButton.innerHTML = "Click to Upload"; 
        exampleButton.parentNode.appendChild(uploadButton); 
 
        uploadButton.onclick = () => { 
            const file = fileInput.files[0]; 
            if(!file){ 
                alert("No file!"); 
                return; 
            } 
 
            // Upload it 
            log("Uploading..."); 
            lt.Document.DocumentFactory.uploadFile(file) 
                .done((uri) => { 
                    // Done, now load it 
                    log("Finished uploading. Now loading..."); 
                    const loadDocumentOptions = new lt.Document.LoadDocumentOptions(); 
                    // Set the name 
                    loadDocumentOptions.name = file.name; 
                    lt.Document.DocumentFactory.loadFromUri(uri, loadDocumentOptions) 
                    .done((doc) => { 
                        log("Document was loaded succesfully"); 
                        log("Name is " + doc.name); 
                        log("MIMEType is " + doc.mimeType); 
                        log("Number of Pages is " + doc.pages.count); 
                    }) 
                    .fail(showServiceError); 
                }) 
                .fail(showServiceError); 
        } 
    } 
} 

Requirements

Target Platforms

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

Leadtools.Document Assembly