WCF 4: Download Files using WCF REST EndPoints
Posted by: Mahesh Sabnis ,
on 6/19/2011,
in
Category Windows Communication Foundation (WCF)
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)

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:
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:
Click on the ‘Service Help page’ and the user friendly url will be displayed:

In the browser, type the url as shown below:
“http://localhost:8081/REST_File_Download/Service.svc/File/ADOEF/png”

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.
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!
Was this article worth reading? Share it with fellow developers too. Thanks!
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