SIGN UP MEMBER LOGIN:    
ARTICLE

Uploading a File Using WCF REST Service

Posted by Dhananjay Kumar Articles | WCF with C# May 02, 2011
Here you will see how to upload a file using WCF REST Service.
Reader Level:


Very often I see one question frequently in all forums, "How to upload a File using WCF REST Service?", so I thought I would write an article about that.

Essentially, there is nothing specific to do to upload file, but the below steps:

  1. Create a service with Post operation
  2. Pass file data as Input Stream
  3. Pass name of the file
  4. Host the service in console application [Note : You can host wherever you want though]

Let us first create a Service Contract.

WCFRestSer1.gif

If you notice, the above service contract is creating a REST resource with POST method. One of the input parameters is Stream.

For your reference, contract interface would look like as below, [You can copy]

IService1.cs

using System.ServiceModel;
using System.ServiceModel.Web;
using System.IO; 
namespace RESTImageUpload
{   
    [ServiceContract]
    public interface IImageUpload
    {
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "FileUpload/{fileName}")]
        void FileUpload(string fileName, Stream fileStream);         
        
    } 
}

Once the contract is defined we need to implement a service to save a file on the server location.

WCFRestSer2.gif

You need to make sure you have the folder FileUpload created on D drive of your server else you would get "Device not ready" exception.

Since the Write() method takes a byte array as input to write data in a file and the input parameter of our service is Stream, we need to do a conversion between Stream to Bye Array.

WCFRestSer3.gif

Final Service implementation would look like below [You can Copy]:

Service1.svc.cs

using System.IO; 
namespace RESTImageUpload
{

    public class ImageUploadService : IImageUpload
    {

       public void FileUpload(string fileName, Stream fileStream)
        {

            FileStream fileToupload = new FileStream("D:\\FileUpload\\" + fileName, FileMode.Create);

            byte[] bytearray = new byte[10000];
            int bytesRead, totalBytesRead = 0;
            do
            {
                bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
                totalBytesRead += bytesRead;
            } while (bytesRead > 0);

            fileToupload.Write(bytearray, 0, bytearray.Length);
            fileToupload.Close();
            fileToupload.Dispose();

        }      

    }
}

So far we are done with the service definition and implementation and now we need to host the service. I am going to host the service in a console application. The host program would have references for System.ServiceModel and System.ServiceModel.Web. If you are not able to find System.ServiceModel.Web reference to add in your console application then change the target framework to .Net Framework 4.0 from .Net Framework 4.0 client profile.

Program.cs

using System;
using System.ServiceModel;
using RESTImageUpload;
using System.ServiceModel.Description;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            ServiceHost host = new ServiceHost(typeof(ImageUploadService), new Uri(baseAddress));
            host.AddServiceEndpoint(typeof(IImageUpload), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
            host.Open();
            Console.WriteLine("Host opened");
            Console.ReadKey(true);
        }
    }
}

Press F5 to run the service hosted in a console application.

WCFRestSer4.gif

The service is now up and running so let us call this service to upload the file.

I am creating a simple ASP.Net Web Application with File Upload control and a button. The ASPX page looks like below:

Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="Button1" runat="server"
        Text="Upload File" />
</asp:Content
>


On the click event of the button we need to make a call to the service to upload the file.

WCFRestSer5.gif

In the above code, I am making a HTTP Web Request and explicitly specifying that the method is POST and content type is text/plain. We need to get the Request Stream and write the byte array in that. As HTTP response you can get the status code.

For your reference the client code would look like as below:

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using System.Net;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += new EventHandler(Button1_Click);
        }

        void Button1_Click(object sender, EventArgs e)
        {
              byte[] bytearray=null ;
              string name = "";
            //throw new NotImplementedException();
            if (FileUpload1.HasFile)
            {
                 name = FileUpload1.FileName;
                Stream stream = FileUpload1.FileContent;
                stream.Seek(0, SeekOrigin.Begin);
                bytearray = new byte[stream.Length];
                int count = 0;
                while (count < stream.Length)
                {
                    bytearray[count++] = Convert.ToByte(stream.ReadByte());
                }

            }
 
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service/FileUpload/";
            HttpWebRequest request = (HttpWebRequestHttpWebRequest.Create(baseAddress+name);          
            request.Method = "POST";
            request.ContentType = "text/plain";
            Stream serverStream  = request.GetRequestStream();
            serverStream.Write(bytearray, 0, bytearray.Length);
            serverStream.Close();
            using ( HttpWebResponse response = request.GetResponse() as HttpWebResponse )   
            {
                int statusCode =(int) response.StatusCode;                              
                StreamReader reader = new StreamReader( response.GetResponseStream() );       

            }
 
        }
    }
}


I hope this post was useful and saved you time to upload a file. In the next post I will show you how to upload an image from a Silverlight client.

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Dhananjay , very nice example. but I am not able to set end points for this example . though i am setting end point and behaviour setting for this example , it is saying that No Endpoint is Set. can you provide any example for setting endpoint.

Posted by Vijay Chaphale Jan 24, 2012

If you provide specific error then I would be in better position to help you

Posted by Dhananjay Kumar Oct 16, 2011

I have tried your guidance but it does not work. Do you mind provide the web config for the reference?

Posted by gary lin Oct 04, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor