Skip to content

File Transfer with WCF: Part II

2009 October 2
tags: , ,
by stefanoricciardi

This is the second post on a small series about transferring large files with WCF using streaming :

A few readers of my previous post on File Transfer with WCF have asked to share the implementation of the UploadFile and DownloadFile methods.

While I can’t share the complete implemention of the service for obvious Intellectual Property reasons, I will outline the basic steps involved in implementing the service. Please refer to my previous post for the details on the DataContracts and OperationContracts involved here.

NOTE: I have extracted these portions from the actual code. I cannot verify this to work as-is, but it should give you a head-start to complete your own service. You should also add your own error checking, and any pre or post-download and/or pre or post-upload processing as required by the business.

Download

public FileDownloadReturnMessage DownloadFile(FileDownloadMessage request)
{
    // parameters validation omitted for clarity
    string localFileName = request.MetaData.LocalFileName;

    try
    {
        string basePath = ConfigurationSettings.AppSettings["FileTransferPath"];
        string serverFileName = Path.Combine(basePath, request.MetaData.RemoteFileName);

        Stream fs = new FileStream(serverFileName, FileMode.Open);

        return new FileDownloadReturnMessage(new FileMetaData(localFileName, serverFileName), fs);
    }
    catch (IOException e)
    {
        throw new FaultException<IOException>(e);
    }
}

Upload

public void UploadFile(FileUploadMessage request)
{
    // parameters validation omitted for clarity
    try
    {
        string basePath = ConfigurationSettings.AppSettings["FileTransferPath"];
        string serverFileName = Path.Combine(basePath, request.MetaData.RemoteFileName);

        using (FileStream outfile = new FileStream(serverFileName, FileMode.Create))
        {
            const int bufferSize = 65536; // 64K

            Byte[] buffer = new Byte[bufferSize];
            int bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);

            while (bytesRead > 0)
            {
                outfile.Write(buffer, 0, bytesRead);
                bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
            }
        }
    }
    catch (IOException e)
    {
        throw new FaultException<IOException>(e);
    }
}

Bookmark and Share

Technorati Tags: , , ,
  • Amit

    Line “string localFileName = request.MetaData.LocalFileName;” should be
    string localFileName = request.FileMetaData.LocalFileName;

  • Amit

    Line “string localFileName = request.MetaData.LocalFileName;” should be
    string localFileName = request.FileMetaData.LocalFileName;

  • raghu

    Hi
    Stefano,
    I need to delete a file at server side after the file is downloaded at client. Kindly help …….

  • raghu

    I am again sending the information from client to the server after download is completed at client. Can I reduce this trip to server from client and still delete the file at server.

  • raghu

    Got it. Implemented using IDISPOSABLE

  • http://www.facebook.com/people/Armando-Vega-Ortiz/1133214447 Armando Vega Ortiz

    I have a little problem and I want some one to help me to figure out a solution for it.

    I am developing a C# ASP.NET 4.0 application that will reside on a Windows Server 2003. By mean of accessing this application through a network computer, users would be able to upload files to the windows server. But also, once these files are stored on server, he/she would be able to copy these files from the windows server to another networked computer.I have found a way to upload files to a specified location on the server disk, but now I need to send these files that are on server disk to other client computers (not the one that is accessing the asp application). Is there any way to do that by mean of using WCF, sockets, etc.?

0 visitors online now
0 guests, 0 bots, 0 members
Max visitors today: 0 at 12:02 am CET
This month: 0 at 02-01-2012 12:00 am CET
This year: 48 at 01-13-2012 02:02 pm CET
All time: 82 at 01-24-2011 04:41 pm CET