Upload and Download File Using BITS from .Net Application

BITS (Background Intelligent Transfer Service) is an service to transfer file (download and upload).

More about BITS you can find at :

  • http://en.wikipedia.org/wiki/Background_Intelligent_Transfer_Service
  • http://msdn.microsoft.com/en-us/library/aa362708%28v=vs.85%29.aspx

    SharpBits is a open source .net base wrapper for BITS API. Sample project for SharpBits you can find here.

    In this post I’ll make a simple project to upload file to website (IIS BITS server extrnsion enabled) and download file from any website.

    For uploading file, please see sample code bellow

       1: static void Main(string[] args)
       2: {
       3:     string remoteFile = @"http://localhost:86/Document to upload.docx";
       4:     string localFile = @"D:\Document to upload.docx";
       5:  
       6:     UploadFile(remoteFile, localFile);
       7: }
       8:  
       9: private static void UploadFile(string remoteFile, string localFile)
      10: {
      11:     if (!string.IsNullOrEmpty(localFile) && File.Exists(localFile))
      12:     {
      13:         var bitsManager = new BitsManager();
      14:  
      15:         var job = bitsManager.CreateJob("uploading file", JobType.Upload);
      16:         job.AddFile(remoteFile, localFile); 
      17:         job.Resume();
      18:     }
      19: }

     

    For downloading file, please see sample code bellow

       1: static void Main(string[] args)
       2: {
       3:     string remoteFile = @"http://www.codeproject.com/KB/IP/sharpBITS/SharpBITSbase.zip";
       4:     string localFile = @"D:\SharpBITSbase.zip";
       5:  
       6:     DownloadFile(remoteFile, localFile);
       7: }
       8:  
       9: private static void DownloadFile(string remoteFile, string localFile)
      10: {
      11:     if (!string.IsNullOrEmpty(remoteFile))
      12:     {
      13:         var bitsManager = new BitsManager();
      14:  
      15:         var job = bitsManager.CreateJob("downloading file", JobType.Download);
      16:         job.AddFile(remoteFile, localFile);
      17:         job.Resume();
      18:     }
      19: }

     

    A download BITS job can contain a lot of files to download, but an upload BITS job only contain single file to upload.

    A BITS job only active (transferring the file) when the job’s owner logged in into to the computer. When you need to transfer the file continuously, please use  one of LOCAL SYSTEM account or LOCAL SERVICE account  or NETWORK SERVICE account as the job’s owner.

    Bits manager also support asynchronous execution method with set the call back method.

       1: private static void UploadFile(string remoteFile, string localFile)
       2: {
       3:     if (!string.IsNullOrEmpty(localFile) && File.Exists(localFile))
       4:     {
       5:         var bitsManager = new BitsManager();                
       6:         bitsManager.OnInterfaceError +=new EventHandler<BitsInterfaceNotificationEventArgs>(bitsManager_OnInterfaceError);
       7:         bitsManager.OnJobAdded +=new EventHandler<NotificationEventArgs>(bitsManager_OnJobAdded);
       8:         bitsManager.OnJobError +=new EventHandler<ErrorNotificationEventArgs>(bitsManager_OnJobError);
       9:         bitsManager.OnJobModified +=new EventHandler<NotificationEventArgs>(bitsManager_OnJobModified);
      10:         bitsManager.OnJobRemoved +=new EventHandler<NotificationEventArgs>(bitsManager_OnJobRemoved);
      11:         bitsManager.OnJobTransferred +=new EventHandler<NotificationEventArgs>(bitsManager_OnJobTransferred);
      12:  
      13:         var job = bitsManager.CreateJob("uploading file", JobType.Upload);
      14:         job.NotificationFlags = NotificationFlags.JobErrorOccured | NotificationFlags.JobModified |
      15:                                 NotificationFlags.JobTransferred;
      16:         job.AddFile(remoteFile, localFile); 
      17:         job.Resume();
      18:     }
      19: }
      20:  
      21: private static void bitsManager_OnJobTransferred(object sender, NotificationEventArgs e)
      22: {
      23:     //transferred job can accessed in : e.Job
      24: }
      25:  
      26: private static void bitsManager_OnJobRemoved(object sender, NotificationEventArgs e)
      27: {
      28:     //removerd job can accessed in : e.Job
      29: }
      30:  
      31: private static void bitsManager_OnJobModified(object sender, NotificationEventArgs e)
      32: {
      33:     //modified job can accessed in : e.Job
      34: }
      35:  
      36: private static void bitsManager_OnJobError(object sender, ErrorNotificationEventArgs e)
      37: {
      38:     //error object can accessed in : e.Error
      39:     //transferred job can accessed in : e.Job
      40: }
      41:  
      42: private static void bitsManager_OnJobAdded(object sender, NotificationEventArgs e)
      43: {
      44:     //transferred job can accessed in : e.Job
      45: }
      46:  
      47: private static void bitsManager_OnInterfaceError(object sender, BitsInterfaceNotificationEventArgs e)
      48: {
      49:     //error description can accessed in : e.Description
      50:     //error message can accessed in : e.Message
      51: }

     

    Commonly used method, property and event on BitsManager class :

    Method :

    - public BitsJob CreateJob(string displayName, JobType jobType) : Create a BITS job.

    - public BitsJobs EnumJobs() and public BitsJobs EnumJobs(JobOwner jobOwner) : Retrieve all BITS jobs.

    Property :

    - public BitsJobs Jobs { get; } : Get all BITS jobs retrieved by method EnumJobs().

    Event :

    - public event EventHandler<BitsInterfaceNotificationEventArgs> OnInterfaceError :

    - public event EventHandler<NotificationEventArgs> OnJobAdded : Callback event raised when a new job added.

    - public event EventHandler<ErrorNotificationEventArgs> OnJobError : Callback event raised when a job error.

    - public event EventHandler<NotificationEventArgs> OnJobModified : Callback event raised when a job state modified.

    - public event EventHandler<NotificationEventArgs> OnJobRemoved : Callback event raised when a job removed (complete or cancel or reset)

    - public event EventHandler<NotificationEventArgs> OnJobTransferred : Callback event raised when a job finish to transferred.

     

    Commonly used method, property and event on BitsJob class :

    Method :

    - public void AddCredentials(BitsCredentials credentials) : Add credential to current BITS job.

    - public void AddFile(BitsFileInfo fileInfo) and public void AddFile(string remoteName, string localName) : Add file to current BITS job.

    - public void Cancel() : Cancel current BITS job.

    - public void Complete() : Complete and delete cuurent finished (transfered) BITS job.

    - public BitsFiles EnumFiles() : Retrieve all files on current BITS jobs.

    - public void RemoveCredentials(AuthenticationTarget target, AuthenticationScheme scheme) and public void RemoveCredentials(BitsCredentials credentials) : Remove credential to current BITS job.

    - public void Resume() : Resume or start current BITS job.

    - public void Suspend() : Suspend or pause current BITS job.

    - public void TakeOwnership() : Take the ownership of current BITS job from other person.

    Property :

    - public string Description { set; get; } : Current job descirption.

    - public string DisplayName { set; get; } : Current job name.

    - public BitsError Error { get; } : Current job error.

    - public uint ErrorCount { get; } : Number of error on current job.

    - public BitsFiles Files { get; } : Get all file on current job retrieved by method EnumFiles().

    - public Guid JobId { get; } : Current job id.

    - public JobTimes JobTimes { get; } : Current job time information.

    - public JobType JobType { get; } : Current job type.

    - public NotificationFlags NotificationFlags { set; get; } : Event notification flag.

    - public string Owner { get; } : Current job owner.

    - public string OwnerName { get; } : Current job owner name.

    - public JobPriority Priority { set; get; } : Current job priority (ForeGround, High, Low, Normal)

    - public JobProgress Progress { get; } : Current job progress.

    - public ProxySettings ProxySettings { get; } : Current job proxy setting (ProxyBypassList, ProxyList, ProxyUsage (AutoDetect, NoProxy, Override, Preconfig) ).

    - public JobState State { get; } : Current job state (Acknowledged, Cancelled, Connecting, Error, Queued, Suspended, Transferred, Transferring, TransientError).

  • Share this post: | | | |
    Published Wednesday, March 16, 2011 7:37 PM by ekasinggih

    Comments

    No Comments
    Powered by Community Server (Commercial Edition), by Telligent Systems