uploadFileDocument Method

Summary

Uploads a new document with options to the server in one shot.

Syntax
TypeScript
JavaScript
uploadFileDocument =function(blob,uploadOptions)

Parameters

blob

The JavaScriptBlob/Fileobject representing the file to upload.

uploadOptions

Options to use with the new document. This value cannot benull.

Return Value

APromiseobject that can resolve successfully to a special uri pointing to the location of the newly-createdLEADDocumentin the cache (verifiable withIsUploadDocumentUri). The returned Promise has a unique type ofAbortableJQueryPromisethat holds theabortmethod.

Remarks

When the value ofUploadDocumentOptions.documentIdisnull(the default), then the document factory will create a new, unique ID using a GUID generator. If the value is notnull, then it is assumed to be a user-defined ID and used as is. In either case, the value is set in theLEADDocument.DocumentIdproperty 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 neither check nor guarantee the uniqueness of these IDs.

TheUploadFileDocumentmethod is an abstraction of three other methods used together to manage the upload of a resource, as follows:

UploadFileDocumentwraps together the functionality of these three methods, and returns a url to the uploaded resource when thePromiseresolves. This url can be used to load the document withLoadFromUri.

The data is uploaded in chunks and the size of each chunk uploaded is determined byuploadBlobChunkSize.

ThePromisefor this method can receive progress events. Refer toDocumentUploadProgressfor more information.

ThePromiseobject that is returned immediately by callingUploadFileDocumentis of typeAbortableJQueryPromise, an extension of the usual jQuery Promise object. This class adds an additional method,Abort, which has the same functionality asAbortUploadDocumentbut is not static, and thus does not need the uri of the uploading file. If the upload is aborted, thefailcallback to thePromiseobject will be called with all three parameters set asnull. SeePromisesfor more information.

This method uses the JavaScriptFileobject to obtain access to a file on the local machine.

Note that the browser must support theFileReader APIin order to manipulate file system data on the local machine. If theFileReaderAPI is not supported, an error will be thrown andIsBrowserErrorwill returntrue.

Refer toUploading Using the Document Libraryfor 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.

UploadFileDocument.ts
DocumentHelper.ts
UploadFileDocument.js
DocumentHelper.js
UploadFileDocument.html
import{ DocumentHelper } from"../../utilities/DocumentHelper";exportclassDocumentFactory_UploadFileDocumentExample {publicconstructor() {lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/v200/LEADTOOLSEVAL.txt","EVAL",null);DocumentHelper.initFactory();}publicrun = (buttonID:string) => {constexampleButton = document.getElementById(buttonID);// Add a file selectconstfileInput = document.createElement("input");fileInput.type ="file";exampleButton.parentNode.appendChild(fileInput);// add an "upload" buttonconstuploadButton = document.createElement("button");uploadButton.type ="button";uploadButton.innerHTML ="Click to Upload";exampleButton.parentNode.appendChild(uploadButton);uploadButton.onclick = () => {constfile = fileInput.files[0];if(!file) {alert("No file!");return;}// Upload itDocumentHelper.log("Uploading...");// Create upload optionslet uploadDocumentOptions =newlt.Document.UploadDocumentOptions();// Enable streaming on the serveruploadDocumentOptions.enableStreaming =true;lt.Document.DocumentFactory.uploadFileDocument(file, uploadDocumentOptions).done((uri) => {// Done, now load itDocumentHelper.log("Finished uploading. Now loading...");constloadDocumentOptions =newlt.Document.LoadDocumentOptions();// Set the nameloadDocumentOptions.name = file.name;lt.Document.DocumentFactory.loadFromUri(uri, loadDocumentOptions).done((doc) => {DocumentHelper.log("Document was loaded successfully");DocumentHelper.log("Name is "+ doc.name);DocumentHelper.log("MIMEType is "+ doc.mimeType);DocumentHelper.log("Number of Pages is "+ doc.pages.count);}).fail(DocumentHelper.showServiceError);}).fail(DocumentHelper.showServiceError);}}}
exportclassDocumentHelper {staticshowServiceError = (jqXHR, statusText, errorThrown) => {alert(“从服务返回错误。看到控制台details.");constserviceError = lt.Document.ServiceError.parseError(jqXHR, statusText, errorThrown);console.error(serviceError);}staticlog = (message:string, data?:any) => {constoutputElement = document.getElementById("output");if(outputElement) {consttime = (newDate()).toLocaleTimeString();consttextElement = document.createElement("p");textElement.innerHTML = (outputElement.childElementCount + 1) +" ["+ time +"]: "+ message;outputElement.insertBefore(textElement, outputElement.firstChild);}if(!data)console.log(message);elseconsole.log(message, data);}staticinitFactory = () => {// To communicate with the DocumentsService, it must be running!// Change these parameters to match the path to the service.lt.Document.DocumentFactory.serviceHost ="http://localhost:40000";lt.Document.DocumentFactory.servicePath ="";lt.Document.DocumentFactory.serviceApiPath ="api";}}
import{ DocumentHelper } from"../../utilities/DocumentHelper";exportclassDocumentFactory_UploadFileDocumentExample {constructor() {lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/v200/LEADTOOLSEVAL.txt","EVAL",null);DocumentHelper.initFactory();}run = (buttonID) => {constexampleButton = document.getElementById(buttonID);// Add a file selectconstfileInput = document.createElement("input");fileInput.type ="file";exampleButton.parentNode.appendChild(fileInput);// add an "upload" buttonconstuploadButton = document.createElement("button");uploadButton.type ="button";uploadButton.innerHTML ="Click to Upload";exampleButton.parentNode.appendChild(uploadButton);uploadButton.onclick = () => {constfile = fileInput.files[0];if(!file) {alert("No file!");return;}// Upload itDocumentHelper.log("Uploading...");// Create upload optionslet uploadDocumentOptions =newlt.Document.UploadDocumentOptions();// Enable streaming on the serveruploadDocumentOptions.enableStreaming =true;lt.Document.DocumentFactory.uploadFileDocument(file, uploadDocumentOptions).done((uri) => {// Done, now load itDocumentHelper.log("Finished uploading. Now loading...");constloadDocumentOptions =newlt.Document.LoadDocumentOptions();// Set the nameloadDocumentOptions.name = file.name;lt.Document.DocumentFactory.loadFromUri(uri, loadDocumentOptions).done((doc) => {DocumentHelper.log("Document was loaded successfully");DocumentHelper.log("Name is "+ doc.name);DocumentHelper.log("MIMEType is "+ doc.mimeType);DocumentHelper.log("Number of Pages is "+ doc.pages.count);}).fail(DocumentHelper.showServiceError);}).fail(DocumentHelper.showServiceError);}}}
exportclassDocumentHelper {staticshowServiceError = (jqXHR, statusText, errorThrown) => {alert(“从服务返回错误。看到控制台details.");constserviceError = lt.Document.ServiceError.parseError(jqXHR, statusText, errorThrown);console.error(serviceError);}staticlog = (message, data) => {constoutputElement = document.getElementById("output");if(outputElement) {consttime = (newDate()).toLocaleTimeString();consttextElement = document.createElement("p");textElement.innerHTML = (outputElement.childElementCount + 1) +" ["+ time +"]: "+ message;outputElement.insertBefore(textElement, outputElement.firstChild);}if(!data)console.log(message);elseconsole.log(message, data);}staticinitFactory = () => {// To communicate with the DocumentsService, it must be running!// Change these parameters to match the path to the service.lt.Document.DocumentFactory.serviceHost ="http://localhost:40000";lt.Document.DocumentFactory.servicePath ="";lt.Document.DocumentFactory.serviceApiPath ="api";}}
"en">Document Example | UploadFileDocument




<链接rel="stylesheet"type="text/css"href="../../css/examples.css">
"output">
"img"/>
Requirements
Target Platforms
Leadtools.Document Assembly
188金宝搏的网址客服|Support|Contact Us|Intellectual Property Notices
© 1991-2021LEAD Technologies, Inc.All Rights Reserved.