title: WCF sending HttpPostedFileBase to service for processing
body: <p>I'm in a need to send a HttpPostedFileBase to a wcf serivce for processing which runs at the server, from the front end of web page after user click "upload a file" button. I used HttpPostedFileBase in the service contract first, but it didn't work. Then I tried to put HttpPostedFileBase into the data contract but it still didn't work. I struggled two days to go through that problem. Now here is the approach:</p>
  
  <p>In Service Contract:</p>
  
  <pre><code>[ServiceContract]
  public interface IFileImportWcf
  {
      [OperationContract]
      string FileImport(byte[] file);
  }
  </code></pre>
  
  <p>And found these two methods to convert byte[] to stream and vice versa.</p>
  
  <pre><code>    public byte[] StreamToBytes(Stream stream)
      {
          byte[] bytes = new byte[stream.Length];
          stream.Read(bytes, 0, bytes.Length);
          stream.Seek(0, SeekOrigin.Begin);
          return bytes;
      }
      public Stream BytesToStream(byte[] bytes)
      {
          Stream stream = new MemoryStream(bytes);
          return stream;
      } 
  </code></pre>
  
  <p>In the controller:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Import(HttpPostedFileBase attachment)
  {
      //convert HttpPostedFileBase to bytes[]
      var binReader = new BinaryReader(attachment.InputStream);
      var file = binReader.ReadBytes(attachment.ContentLength);
      //call wcf service
      var wcfClient = new ImportFileWcfClient();
      wcfClient.FileImport(file);
  }
  </code></pre>
  
  <p>My question is: What's the better way to send a HttpPostedFileBase to a wcf service?</p>