Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / Visual Basic

Progress Indication while Uploading/Downloading Files using WCF

Rate me:
Please Sign up or sign in to vote.
4.92/5 (66 votes)
4 Feb 2009CPOL5 min read 511.5K   14.6K   244  
This article examines the implementation of upload and download functionality with progress indication (progress bar feature) using Windows Communication Foundation.
/// Notice : Code written by Dimitris Papadimitriou - http://www.papadi.gr
/// Code is provided to be used freely but without any warranty of any kind
using System;
using System.ServiceModel;

namespace FileService
{
    [ServiceContract]
    public interface IFileTransferService
    {
        [OperationContract]
        void UploadFile(RemoteFileInfo request);

        [OperationContract]
        RemoteFileInfo DownloadFile(DownloadRequest request);
    }

    [MessageContract]
    public class DownloadRequest
    {
        [MessageBodyMember]
        public string FileName;
    }

    [MessageContract]
    public class RemoteFileInfo: IDisposable
    {
        [MessageHeader(MustUnderstand = true)]
        public string FileName;

        [MessageHeader(MustUnderstand = true)]
        public long Length;

        [MessageBodyMember(Order = 1)]
        public System.IO.Stream FileByteStream;

        public void Dispose()
        {
            // close stream when the contract instance is disposed. this ensures that stream is closed when file download is complete, since download procedure is handled by the client and the stream must be closed on server.
            // thanks Bhuddhike! http://blogs.thinktecture.com/buddhike/archive/2007/09/06/414936.aspx
            if (FileByteStream!=null)
            {
                FileByteStream.Close();
                FileByteStream = null;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Luxembourg Luxembourg
Dimitris Papadimitriou is a Software Development Professional specialized in Microsoft technologies. He has been awarded with the Microsoft MVP award (Connected System Developer). He lives in Luxembourg. Read more in his personal web page.

Comments and Discussions