←Select platform

BeginUpload Method

Summary

Starts uploading a document to the cache.

Syntax
C#
VB
C++
Java
public static Uri BeginUpload( 
   UploadDocumentOptions options 
) 
Public Shared Function BeginUpload( 
   ByVal options As UploadDocumentOptions 
) As System.Uri 
public:  
   static System::Uri^ BeginUpload( 
      UploadDocumentOptions^ options 
   ) 
public static URI beginUpload(UploadDocumentOptions options) 

Parameters

options

Options to use with the new document. This value cannot be null.

Return Value

The temporary URL for the uploaded document.

Remarks

This method will throw an exception if neither UploadDocumentOptions.Cache nor Cache has been set up with a valid cache object.

BeginUpload, UploadDocument, and AbortUploadDocument can be used to upload a document in chunks to the cache used by this DocumentFactory. After the document is uploaded, you can use LoadFromUri to create a LEADDocument object from the data.

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

This method returns a URL with a special LEAD cache scheme to indicate a loaded document. This URL can be detected using IsUploadDocumentUri helper method.

Refer to Uploading Using the Document Library for detailed information on how to use these methods and the various options available.

Example
C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 
 
using Leadtools.Document; 
using Leadtools.Caching; 
using Leadtools.Annotations.Engine; 
using Leadtools.Ocr; 
using Leadtools.Barcode; 
using Leadtools.Document.Converter; 
 
public void DocumentFactoryBeginUploadExample() 
{ 
	var cache = GetCache(); 
 
	// Upload the document in 32K chunks 
	const int bufferSize = 1024 * 32; 
	var buffer = new byte[bufferSize]; 
 
	var fileName = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf"); 
	Uri uploadUri = null; 
	using (var reader = File.OpenRead(fileName)) 
	{ 
		// Start uploading 
		var uploadOptions = new UploadDocumentOptions(); 
		uploadOptions.Cache = cache; 
		// Optional: set the mime type of the document we are uploading. 
		// If this value is not provided or is wrong then it will be overridden by the factory when LoadFromUri is called. 
		uploadOptions.MimeType = "application/pdf"; 
		// Optional: set the name of the document, the same name as the file. 
		// This value is only important as a hint when the document file data does not contain a valid signature, such as TXT or XML files 
		uploadOptions.Name = "Leadtools.pdf"; 
		uploadUri = DocumentFactory.BeginUpload(uploadOptions); 
 
		int bytes; 
		do 
		{ 
			bytes = reader.Read(buffer, 0, bufferSize); 
			if (bytes > 0) 
			{ 
				// Upload a chunk 
				DocumentFactory.UploadDocument(cache, uploadUri, buffer, 0, bytes); 
			} 
		} 
		while (bytes > 0); 
	} 
 
	// We are done, this method is optional, we can call LoadFromUri right now 
	DocumentFactory.EndUpload(cache, uploadUri); 
 
	// Now load the document 
	var options = new LoadDocumentOptions(); 
	options.Cache = cache; 
	using (var document = DocumentFactory.LoadFromUri(uploadUri, options)) 
	{ 
		PrintOutDocumentInfo(document); 
	} 
} 
 
 
public void PrintOutDocumentInfo(LEADDocument document) 
{ 
	Console.WriteLine("General"); 
	Console.WriteLine("  DocumentId:" + document.DocumentId); 
	if (document.Uri != null) 
		Console.WriteLine("  Uri:" + document.Uri); 
	Console.WriteLine("  Name:" + document.Name); 
	Console.WriteLine("  CacheStatus:" + document.CacheStatus); 
	Console.WriteLine("  LastCacheSyncTime:" + document.LastCacheSyncTime); 
	Console.WriteLine("  IsReadOnly:" + document.IsReadOnly); 
	Console.WriteLine("  IsLocal:" + document.IsLocal); 
	Console.WriteLine("  MimeType:" + document.MimeType); 
	Console.WriteLine("  IsEncrypted:" + document.IsEncrypted); 
	Console.WriteLine("  IsDecrypted:" + document.IsDecrypted); 
	Console.WriteLine("  UserData:" + document.UserData); 
	Console.WriteLine("Cache"); 
	Console.WriteLine("  HasCache:" + document.HasCache); 
	Console.WriteLine("  AutoDeleteFromCache:" + document.AutoDeleteFromCache); 
	Console.WriteLine("Metadata"); 
	foreach (var item in document.Metadata) 
		Console.WriteLine("  {0} {1}", item.Key, item.Value); 
 
	Console.WriteLine("Documents"); 
	Console.WriteLine("  Count:" + document.Documents.Count); 
	foreach (var childDocument in document.Documents) 
	{ 
		Console.WriteLine("    Name:" + childDocument.Name); 
	} 
 
	Console.WriteLine("Pages"); 
	Console.WriteLine("  Count:" + document.Pages.Count); 
 
	for (var pageNumber = 1; pageNumber <= document.Pages.Count; pageNumber++) 
	{ 
		var page = document.Pages[pageNumber - 1]; 
 
		Console.WriteLine("    PageNumber:" + pageNumber); 
		Console.WriteLine("      OriginalPageNumber:" + page.OriginalPageNumber); 
		Console.WriteLine("      OriginalDocumentName:" + page.Document.Name); 
		Console.WriteLine("      Size:{0}", page.Size); 
	} 
 
	Console.WriteLine("--------"); 
} 
 
 
		public ObjectCache GetCache() 
{ 
	// Create a LEADTOOLS FileCache object 
 
	var cacheDir = Path.Combine(LEAD_VARS.ImagesDir, "cache"); 
	if (Directory.Exists(cacheDir)) 
		Directory.Delete(cacheDir, true); 
 
	Directory.CreateDirectory(cacheDir); 
 
	var cache = new FileCache(); 
	cache.CacheDirectory = cacheDir; 
 
	return cache; 
} 
 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS21\Resources\Images"; 
} 
 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS21\Resources\Images"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Document.Writer 
Imports Leadtools.Svg 
Imports Leadtools.Document 
Imports Leadtools.Caching 
Imports Leadtools.Annotations.Engine 
Imports Leadtools.Barcode 
Imports Leadtools.Ocr 
Imports LeadtoolsDocumentExamples.LeadtoolsExamples.Common 
Imports Leadtools.Document.Converter 
 
Public Shared Sub DocumentFactoryBeginUploadExample() 
   Dim cache As ObjectCache = GetCache() 
 
   ' Upload the document in 32K chunks 
   Const bufferSize As Integer = 1024 * 32 
   Dim buffer() As Byte = New Byte(bufferSize - 1) {} 
 
   Dim fileName As String = Path.Combine(ImagesPath.Path, "Leadtools.pdf") 
   Dim uploadUri As Uri = Nothing 
   Using reader As FileStream = File.OpenRead(fileName) 
      ' Start uploading 
      Dim uploadOptions As New UploadDocumentOptions() 
      uploadOptions.Cache = cache 
      ' Optional: set the mime type of the document we are uploading. 
      ' If this value Is Not provided Or Is wrong then it will be overridden by the factory when LoadFromUri Is called. 
      uploadOptions.MimeType = "application/pdf" 
      ' Optional: set the name of the document, the same name as the file. 
      ' This value Is only important as a hint when the document file data does Not contain a valid signature, such as TXT Or XML files 
      uploadOptions.Name = "Leadtools.pdf" 
      uploadUri = DocumentFactory.BeginUpload(uploadOptions) 
 
      Dim bytes As Integer 
      Do 
         bytes = reader.Read(buffer, 0, bufferSize) 
         If bytes > 0 Then 
            ' Upload a chunk 
            DocumentFactory.UploadDocument(cache, uploadUri, buffer, 0, bytes) 
         End If 
      Loop While bytes > 0 
   End Using 
 
   ' We are done, this method Is optional, we can call LoadFromUri right now 
   DocumentFactory.EndUpload(cache, uploadUri) 
 
   ' Now load the document 
   Dim options As New LoadDocumentOptions() 
   options.Cache = cache 
   Using document As Leadtools.Document.LEADDocument = DocumentFactory.LoadFromUri(uploadUri, options) 
      PrintOutDocumentInfo(document) 
   End Using 
End Sub 
 
Public Shared Sub PrintOutDocumentInfo(ByVal document As LEADDocument) 
   Console.WriteLine("General") 
   Console.WriteLine("  DocumentId:" + document.DocumentId) 
   If Not IsNothing(document.Uri) Then 
      Console.WriteLine("  Uri:" + document.Uri.ToString()) 
   Else 
      Console.WriteLine("  Name:" + document.Name) 
   End If 
 
   Console.WriteLine("  CacheStatus:" + document.CacheStatus.ToString()) 
   Console.WriteLine("  LastCacheSyncTime:" + document.LastCacheSyncTime.ToString()) 
   Console.WriteLine("  IsReadOnly:" + document.IsReadOnly.ToString()) 
   Console.WriteLine("  IsLocal:" + document.IsLocal.ToString()) 
   Console.WriteLine("  MimeType:" + document.MimeType) 
   Console.WriteLine("  IsEncrypted:" + document.IsEncrypted.ToString()) 
   Console.WriteLine("  IsDecrypted:" + document.IsDecrypted.ToString()) 
   If Not IsNothing(document.UserData) Then 
      Console.WriteLine("  UserData:" + document.UserData.ToString()) 
   End If 
   Console.WriteLine("Cache") 
   Console.WriteLine("  HasCache:" + document.HasCache.ToString()) 
   Console.WriteLine("  AutoDeleteFromCache:" + document.AutoDeleteFromCache.ToString()) 
   Console.WriteLine("Metadata") 
   For Each item As KeyValuePair(Of String, String) In document.Metadata 
      Console.WriteLine("  {0} {1}", item.Key, item.Value) 
   Next 
 
   Console.WriteLine("Documents") 
   Console.WriteLine("  Count:" + document.Documents.Count.ToString()) 
   For Each childDocument As LEADDocument In document.Documents 
      Console.WriteLine("    Name:" + childDocument.Name) 
   Next 
 
   Console.WriteLine("Pages") 
   Console.WriteLine("  Count:" + document.Pages.Count.ToString()) 
 
   For pageNumber As Integer = 1 To document.Pages.Count 
      Dim page As Leadtools.Document.DocumentPage = document.Pages(pageNumber - 1) 
 
      Console.WriteLine("    PageNumber:" + pageNumber.ToString()) 
      Console.WriteLine("      OriginalPageNumber:" + page.OriginalPageNumber.ToString()) 
      Console.WriteLine("      OriginalDocumentName:" + page.Document.Name) 
      Console.WriteLine("      Size:{0}", page.Size.ToString()) 
   Next 
 
   Console.WriteLine("--------") 
End Sub 
 
Public Shared Function GetCache() As ObjectCache 
   ' Create a LEADTOOLS FileCache object 
 
   Dim cacheDir As String = Path.Combine(ImagesPath.Path, "cache") 
   If Directory.Exists(cacheDir) Then 
      Directory.Delete(cacheDir, True) 
   End If 
 
   Directory.CreateDirectory(cacheDir) 
 
   Dim cache As New FileCache() 
   cache.CacheDirectory = cacheDir 
 
   Return cache 
End Function 
Requirements
Target Platforms
Help Version 21.0.2021.7.2
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.