LEADTOOLS SharePoint Client (Leadtools.SharePoint.Client assembly)
LEAD Technologies, Inc

DownloadFileAsync Method

Example 





The full URI for the source item in the SharePoint server. This value cannot be null (Nothing in Visual Basic). It must be the URI to a valid SharePoint item, for example http://server/Shared Documents/File.ext
The destination file name to download the item to. If the file already exists, it will be overwritten. This value cannot be null (Nothing in Visual Basic).
The optional user-supplied state object that is used to identify the task that raised the Leadtools.SharePoint.Client.SharePointClient.DownloadCompleted event.
Downloads an item asynchronously from a SharePoint server to a disk.
Syntax
public void DownloadFileAsync( 
   Uri sourceUri,
   string destinationFileName,
   object userState
)
'Declaration
 
Public Sub DownloadFileAsync( _
   ByVal sourceUri As Uri, _
   ByVal destinationFileName As String, _
   ByVal userState As Object _
) 
'Usage
 
Dim instance As SharePointClient
Dim sourceUri As Uri
Dim destinationFileName As String
Dim userState As Object
 
instance.DownloadFileAsync(sourceUri, destinationFileName, userState)
public void DownloadFileAsync( 
   Uri sourceUri,
   string destinationFileName,
   object userState
)
 function Leadtools.SharePoint.Client.SharePointClient.DownloadFileAsync( 
   sourceUri ,
   destinationFileName ,
   userState 
)
public:
void DownloadFileAsync( 
   Uri^ sourceUri,
   String^ destinationFileName,
   Object^ userState
) 

Parameters

sourceUri
The full URI for the source item in the SharePoint server. This value cannot be null (Nothing in Visual Basic). It must be the URI to a valid SharePoint item, for example http://server/Shared Documents/File.ext
destinationFileName
The destination file name to download the item to. If the file already exists, it will be overwritten. This value cannot be null (Nothing in Visual Basic).
userState
The optional user-supplied state object that is used to identify the task that raised the Leadtools.SharePoint.Client.SharePointClient.DownloadCompleted event.
Remarks

This method will return control immediately to your program and the Leadtools.SharePoint.Client.SharePointClient.DownloadCompleted event will occur when the download operation is completed (whether or not the operation is successful).

To download a file synchronously, use the DownloadFile method.

To download an item to a stream, use the GetDownloadStream or GetDownloadStreamAsync method.

To upload an item to SharePoint, use the UploadFile, UploadFileAsync, UploadStream or UploadStreamAsync method.

Example
 
Private Shared Sub SharePointClientDownloadFileAsyncExample()
   ' Replace SHAREPOINT_SITE_URI with a valid URL to a SharePoint site, for example
   ' http://SiteCollection/MySite
   Dim siteUri As New Uri(SHAREPOINT_SITE_URI)
   ' Replace SHAREPOINT_FOLDER_NAME with a valid folder on the site above, for example
   ' "Documents" or "Documents\Sub Documents"
   Dim folderName As String = SHAREPOINT_FOLDER_NAME

   ' Replace SHAREPOINT_DOCUMENT_NAME with a valid document on the folder above, for example
   ' MyDocument.tif
   Dim documentName As String = SHAREPOINT_DOCUMENT_NAME

   ' Build the full URL to the document are we going to download
   Dim builder As New UriBuilder(siteUri)
   builder.Path = Path.Combine(builder.Path, folderName)
   builder.Path = Path.Combine(builder.Path, documentName)
   Dim sourceDocumentUri As Uri = builder.Uri

   Dim spClient As New SharePointClient()

   ' Optional: Set the credentials:
   spClient.Credentials = New NetworkCredential(USER_NAME, PASSWORD, DOMAIN)

   Dim tempFileName As String = Path.GetTempFileName()

   ' If this is a console application demo, we might exit the program before the operation completes,
   ' so use a wait handle to not exit this method till the opreation completes
   Dim wait As New AutoResetEvent(False)

   ' Download the SharePoint item to the temporary file asynchronously
   AddHandler spClient.DownloadCompleted, AddressOf MyDownloadFileAsyncCompleted
   spClient.DownloadFileAsync(sourceDocumentUri, tempFileName, wait)

   ' Wait till the operation completes
   wait.WaitOne()
   wait.Close()

   ' Delete the temporary file we created
   File.Delete(tempFileName)
End Sub

Private Shared Sub MyDownloadFileAsyncCompleted(ByVal sender As Object, ByVal e As SharePointClientDownloadCompletedEventArgs)
   ' Remove our handler
   Dim spClient As SharePointClient = CType(sender, SharePointClient)
   RemoveHandler spClient.DownloadCompleted, AddressOf MyDownloadFileAsyncCompleted

   If IsNothing(e.Error) AndAlso Not e.Cancelled Then
      ' All is OK, use RasterCodecs to get its info
      Using codecs As New RasterCodecs()
         Using imageInfo As CodecsImageInfo = codecs.GetInformation(e.DestinationFileName, True)
            ' Show the image info
            Console.WriteLine("URL:        {0}", e.SourceUri)
            Console.WriteLine("Pages:      {0}", imageInfo.TotalPages)
            Console.WriteLine("Size:       {0} by {1} pixels", imageInfo.Width, imageInfo.Height)
            Console.WriteLine("Resolution: {0} by {1} dpi", imageInfo.XResolution, imageInfo.YResolution)
            Console.WriteLine("Bits/Pixel: {0}", imageInfo.BitsPerPixel)
         End Using
      End Using
   Else
      ' Some error occured
      If Not IsNothing(e.Error) Then
         Console.WriteLine(e.Error.Message)
      Else
         Console.WriteLine("User cancelled")
      End If
   End If

   ' Tell whoever is listening that we are done
   Dim wait As EventWaitHandle = CType(e.UserState, EventWaitHandle)
   wait.Set()
End Sub
private static void SharePointClientDownloadFileAsyncExample()
{
   // Replace SHAREPOINT_SITE_URI with a valid URL to a SharePoint site, for example
   // http://SiteCollection/MySite
   Uri siteUri = new Uri(SHAREPOINT_SITE_URI);
   // Replace SHAREPOINT_FOLDER_NAME with a valid folder on the site above, for example
   // "Documents" or "Documents\Sub Documents"
   string folderName = SHAREPOINT_FOLDER_NAME;

   // Replace SHAREPOINT_DOCUMENT_NAME with a valid document on the folder above, for example
   // MyDocument.tif
   string documentName = SHAREPOINT_DOCUMENT_NAME;

   // Build the full URL to the document are we going to download
   UriBuilder builder = new UriBuilder(siteUri);
   builder.Path = Path.Combine(builder.Path, folderName);
   builder.Path = Path.Combine(builder.Path, documentName);
   Uri sourceDocumentUri = builder.Uri;

   SharePointClient spClient = new SharePointClient();

   // Optional: Set the credentials:
   spClient.Credentials = new NetworkCredential(USER_NAME, PASSWORD, DOMAIN);

   string tempFileName = Path.GetTempFileName();

   // If this is a console application demo, we might exit the program before the operation completes,
   // so use a wait handle to not exit this method till the opreation completes
   AutoResetEvent wait = new AutoResetEvent(false);

   // Download the SharePoint item to the temporary file asynchronously
   spClient.DownloadCompleted += new EventHandler<SharePointClientDownloadCompletedEventArgs>(MyDownloadFileAsyncCompleted);
   spClient.DownloadFileAsync(sourceDocumentUri, tempFileName, wait);

   // Wait till the operation completes
   wait.WaitOne();
   wait.Close();

   // Delete the temporary file we created
   File.Delete(tempFileName);
}

private static void MyDownloadFileAsyncCompleted(object sender, SharePointClientDownloadCompletedEventArgs e)
{
   // Remove our handler
   SharePointClient spClient = sender as SharePointClient;
   spClient.DownloadCompleted -= new EventHandler<SharePointClientDownloadCompletedEventArgs>(MyDownloadFileAsyncCompleted);

   if (e.Error == null && !e.Cancelled)
   {
      // All is OK, use RasterCodecs to get its info
      using (RasterCodecs codecs = new RasterCodecs())
      {
         using (CodecsImageInfo imageInfo = codecs.GetInformation(e.DestinationFileName, true))
         {
            // Show the image info
            Console.WriteLine("URL:        {0}", e.SourceUri);
            Console.WriteLine("Pages:      {0}", imageInfo.TotalPages);
            Console.WriteLine("Size:       {0} by {1} pixels", imageInfo.Width, imageInfo.Height);
            Console.WriteLine("Resolution: {0} by {1} dpi", imageInfo.XResolution, imageInfo.YResolution);
            Console.WriteLine("Bits/Pixel: {0}", imageInfo.BitsPerPixel);
         }
      }
   }
   else
   {
      // Some error occured
      if (e.Error != null)
         Console.WriteLine(e.Error.Message);
      else
         Console.WriteLine("User cancelled");
   }

   // Tell whoever is listening that we are done
   EventWaitHandle wait = e.UserState as EventWaitHandle;
   wait.Set();
}
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

SharePointClient Class
SharePointClient Members

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.