uploadFile Method

Summary

Uploads a new document to the server.

Syntax

JavaScript Syntax
uploadFile = function(file, documentId) 
TypeScript Syntax
static uploadFile(file: any, documentId: string): JQueryPromise<any>; 

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 Document in the cache (verifiable with IsUploadDocumentUri). The returned Promise has a unique type of AbortableJQueryPromise that holds a 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 Document.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 Documents 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 in Document and replace the example function call to the function below.

JavaScript Example
function uploadFileExample() { 
   // Assume "file" is an HTML file object in the page 
   // Get the File object to upload 
   var file = $("#file").get()[0].files[0]; 
   // Upload it 
   console.log("Uploading ..."); 
   lt.Documents.DocumentFactory.uploadFile(file) 
       .done(function (uri) { 
          // Done, now load it 
          console.log("Finished, loading"); 
          var loadDocumentOptions = new lt.Documents.LoadDocumentOptions(); 
          // Set the name 
          loadDocumentOptions.name = file.name; 
          lt.Documents.DocumentFactory.loadFromUri(uri, loadDocumentOptions) 
              .done(function (doc) { 
                 console.log("Document was loaded succesfully"); 
                 console.log("Name is " + doc.name); 
                 console.log("MIMEType is " + doc.mimeType); 
                 console.log("Number of Pages is " + doc.pages.count); 
              }); 
       }); 
} 

Requirements

Target Platforms

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

Leadtools.Documents Assembly