WCF 4: Download Files using WCF REST EndPoints

Posted by: Mahesh Sabnis , on 6/19/2011, in Category Windows Communication Foundation (WCF)
Views: 140483
Abstract: This article demonstrates how to make use of WCF REST service for downloading files.

While discussing about the content download facility development using WCF with one of my clients, I proposed the solution of defining operation contracts with a Stream return type from WCF services and consuming it in client application. However the client’s requirement was to make use of the WCF REST service for file downloading e.g. for .jpg, png, docs etc. WCF REST services provides XML or JSON conversion for the response from the WCF service , however the question that was bothering me was how to make use of REST for file download?


After searching the System.ServiceModel.Web namespace, I found the ‘WebOperationContext’ class. This is a helper class that provides easy access to the contextual properties of the Web request and response communication. Then while working with this class I found that using the WebOperationContext.Current.OutgoingResponse.ContentType, the content type header from the outgoing web response can be managed with the value: “application/octet-stream”. This represents  a generic value for Raw binary data which can contain format like, png,jpg,zip,doc etc. So I thought to make a use of this class for our application

Step 1: Open VS2010 and create a WCF service application and name it ‘WCF_FileDownload_REST’. Add a folder named ‘FileServer’ in the application with following files: (Note: You can enter your files here)

wcf filedownload REST

Step 2: Rename IService1.cs to IService.cs and Service1.svc to Service.svc. Open IService.cs and write the following ServiceContract in it:



The above code shows the OperationContract with WebGet attribute. The WebGet attribute is set with UriTemplate property to make the REST url user friendly.

Step 3: Open Service.svc.cs and implement the IService in the service class as shown below:

 WCF iService


The DownloadFile method accepts two parameters fileName and fileExpension. The code then generates the file path using FileServer folder in the WCF application and generates the outgoing response using the raw binary format.

Step 4 : Right-Click on Service.svc and select View Markup and change the @ServiceHost as below:



Step 5: Open Web.Config file and add the webHttpBinding protocol mapping so that the WCF REST endpoint can be exposed and also define the Help page for the WCF service as below:


Step 6: Publish the WCF service on your IIS web server and view the Service.svc in the browser. The result should be as shown below:

WCF Rest Service

Click on the ‘Service Help page’ and the user friendly url will be displayed:


In the browser, type the url as shown below:



Here the file is ADOEF.png. Similarly also try to download the Docx/Excel/zip file and you should get a similar download experience.

The advantage of this approach is that the client who knows about this can now directly get connected to your WCF File Download service and use the download facility without using any file IO code.

The entire source code of this article can be download over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions), and Front-end technologies like Angular and React. Follow him on twitter @maheshdotnet or connect with him on LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Lukas Domagala on Sunday, June 19, 2011 8:55 AM
shouldn´t this scenario be even easier and more transperant using wcf webapi from http://wcf.codeplex.com/ ?
Comment posted by Mahesh Sabnis on Sunday, June 19, 2011 11:16 PM
Dear Lukas Domagala,
  I will definately explore the WCF Web APIs for the above scenario. Thanks a lot for this valuable information.
Mahesh Sabnis
Comment posted by Avinash on Monday, July 11, 2011 1:38 AM
I am trying to write client to consume the Web Service which is based on REST.
I can consume it in .NET 3.5 or .NET 4.0.
I was able to consume it in .NET 3.5 using REST Started Kit.
Can same be used for .NET 4.0?
Comment posted by Carlos on Saturday, February 18, 2012 4:49 PM
I am trying to implement your code sample.  I am having issue at AspNetCompatibilityRequirements.Allowed.  Error says AspNetCompatibilityRequirements does not exist in teh current context.

Also, My service has an App.config file not web.config file.  Should I add what you have in the web.config file to my app.config?
Comment posted by Blop on Sunday, November 18, 2012 7:14 AM
if you want to add a filename on your headers follow this post http://outcoldman.com/en/blog/show/215

thx for your article.
Comment posted by TechiRA on Wednesday, January 2, 2013 7:36 AM
It is working only where there is a single operation contract in the service,how should we call this if we have multiple operation contracts.
Comment posted by ThisGuy! on Monday, June 10, 2013 3:52 PM
This is good information, but what about the client-side?  How can I have a client-side HTML5/Javascript client download and do something with the file instead of just allowing the browser to prompt the user to do something with it?
Comment posted by Kulesh Jha on Tuesday, June 25, 2013 5:58 AM
Hi Mahesh,
I attended your WCF Training this Saturday in MIND-Infotech (hope you remember) and after that I had implemented the above article successfully using Iframes in Web Page.
But I would like to add an Exception handling to the WCF method(for e.g. file not found etc.) and how to send an Exception message to the Client with the Exception Details.
Currently, I am not getting any Clue.'
Please Help...
Comment posted by harsh on Monday, October 7, 2013 5:50 AM
superb tutorial...keep it up  thanks a lot...worked fine for me
Comment posted by ravindra on Thursday, March 20, 2014 2:08 AM
can you please provide some materials for downloading multiple files in base64 at a time