title: File upload in WCF Service (413) Request Entity Too Large
body: <p>I'm trying to implement file upload into my WCF Service in asp .net C#</p>
  
  <p>Here is the code in WCF Server for File uploading.</p>
  
  <pre><code> public void FileUpload(string fileName, Stream fileStream)
         {
             FileStream fileToupload = new FileStream("D:\\FileUpload\\" + fileName, FileMode.Create);
  
          //   byte[] bytearray = new byte[10000];
             byte[] bytearray = new byte[1000];
             int bytesRead, totalBytesRead = 0;
             do
             {
                 bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
                 totalBytesRead += bytesRead;
                 if(bytesRead &gt; 0)
                 fileToupload.Write(bytearray, 0, bytearray.Length);
  
             } while (bytesRead &gt; 0);
  
           //  fileToupload.Write(bytearray, 0, bytearray.Length);
             fileToupload.Close();
             fileToupload.Dispose();
  
  
  
         }
  </code></pre>
  
  <p>Here is the code for Client to upload a File: <strong>(Fixed Byte Array Size)</strong></p>
  
  <pre><code>protected void bUpload_Click(object sender, EventArgs e)
  {
      byte[] bytearray = null;
      Stream stream;
      string fileName = "";
      //throw new NotImplementedException();
      if (FileUpload1.HasFile)
      {
          fileName = FileUpload1.FileName;
          stream = FileUpload1.FileContent;
          stream.Seek(0, SeekOrigin.Begin);
          bytearray = new byte[stream.Length];
          int count = 0;
          while (count &lt; stream.Length)
          {
              bytearray[count++] = Convert.ToByte(stream.ReadByte());
          }
  
      }
  
      string baseAddress = "http://localhost/WCFService/Service1.svc/FileUpload/";
  
      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress + fileName);
      request.Method = "POST";
      request.ContentType = "text/plain";
      Stream serverStream = request.GetRequestStream();
      serverStream.Write(bytearray, 0, bytearray.Length);
      serverStream.Close();
      try
      {
          using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
          {
              int statusCode = (int)response.StatusCode;
              System.Diagnostics.Debug.WriteLine("statusCode: " + statusCode);
  
              StreamReader reader = new StreamReader(response.GetResponseStream());
              System.Diagnostics.Debug.WriteLine("reader: " + reader.ToString());
  
          }
      }
      catch (Exception ex)
      {
          System.Diagnostics.Debug.WriteLine("--- EXCEPTION ---");
          ex.ToString();
      }
  }
  </code></pre>
  
  <p>This is working good with a small size file, When I tried with bigger size files
  I changed fixed size byte array to a dynamic byte array Writing to stream.
  Here is updated code: <strong>(chunks of 1024 bytes of Byte array used to Send data)</strong></p>
  
  <pre><code>        request.Method = "POST";
          request.ContentType = "text/plain";
         // Stream serverStream = request.GetRequestStream();
  
          if (FileUpload1.HasFile)
          {
              fileName = FileUpload1.FileName;
              stream = FileUpload1.FileContent;
              stream.Seek(0, SeekOrigin.Begin);
              bytearray = new byte[1024];//stream.Length];
  
  
          }
          int TbyteCount = 0;
          Stream requestStream = request.GetRequestStream();
  
              int bufferSize = 1024;
              byte[] buffer = new byte[bufferSize];
              int byteCount = 0;
              while ((byteCount = stream.Read(buffer, 0, bufferSize)) &gt; 0)
              {
                  TbyteCount = TbyteCount + byteCount; 
                  requestStream.Write(buffer, 0, byteCount);
              }
  
              requestStream.Close();
  
  
          try
          {
              using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
              {
                  int statusCode = (int)response.StatusCode;
                  System.Diagnostics.Debug.WriteLine("statusCode: " + statusCode);
  
                  StreamReader reader = new StreamReader(response.GetResponseStream());
                  System.Diagnostics.Debug.WriteLine("reader: " + reader.ToString());
  
              }
          }
          catch (Exception ex)
          {
              System.Diagnostics.Debug.WriteLine("--- EXCEPTION ---");
              ex.ToString();
          }
      }
  </code></pre>
  
  <p>But While reading response I get Exception 
  <strong>The remote server returned an error: (413) Request Entity Too Large.</strong></p>
  
  <p>Am I doing correct with writing multiple times in request stream !? </p>
  
  <p><strong>I used a file size 22.4 KB, it is successfully uploaded using 1st code (fixed size array) 
  If I split file size in multiples of 1024 bytes and tried to send then there is a problem.</strong></p>
  
  <p>Web.config file</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
    &lt;appSettings&gt;
      &lt;add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/&gt;
    &lt;/appSettings&gt;
    &lt;system.web&gt;
      &lt;compilation debug="true"/&gt;
    &lt;/system.web&gt;
    &lt;system.serviceModel&gt;
      &lt;services&gt;
        &lt;service name="WcfServiceApp.Service1" behaviorConfiguration="ServiceBehavior"&gt;
          &lt;endpoint address="" binding="webHttpBinding" contract="WcfServiceApp.IService1" behaviorConfiguration="webBehaviour"/&gt;
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress="http://localhost:50327/Service1.svc"/&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="ServiceBehavior"&gt;
            &lt;!-- To avoid disclosing metadata information, set the values below to false before deployment --&gt;
            &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/&gt;
            &lt;!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information --&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name="webBehaviour"&gt;
            &lt;webHttp/&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;!--&lt;protocolMapping&gt;
        &lt;add binding="basicHttpsBinding" scheme="https"/&gt;
      &lt;/protocolMapping&gt;--&gt;
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/&gt;
    &lt;/system.serviceModel&gt;
    &lt;system.webServer&gt;
      &lt;httpProtocol&gt;
        &lt;customHeaders&gt;
          &lt;add name="Access-Control-Allow-Origin" value="*"/&gt;
          &lt;add name="Access-Control-Allow-Headers" value="Content-Type, Accept"/&gt;
        &lt;/customHeaders&gt;
      &lt;/httpProtocol&gt;
      &lt;directoryBrowse enabled="true"/&gt;
    &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>