title: How to Upload a Photo from ASP.NET MVC to a WCF Web Service
body: <p>Hi I am trying to call a WCF web service from a default ASP.NET MVC project, in order to upload a photo to the server from the ASP.NET MVC website.
  My WCF web service look like this:</p>
  
  <pre><code>public void SaveImage(string fileName, byte[] photo)
      {
          writeByteArrayToFile(Path.Combine(@"c:\TempImg", fileName), photo);
      }
  
      public void writeByteArrayToFile(string fileName, byte[] buffer)
      {
          using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
          {
              fs.Write(buffer, 0, (int)buffer.Length);
          }
      }
  </code></pre>
  
  <p>In the HomeController on the project I have written this code:</p>
  
  <pre><code>using HackedTwo.ServiceReference1;
  
  namespace HackedTwo.Controllers
  {
      [HandleError]
      public class HomeController : Controller
      {
          private ServiceReference1.IService1 myWS;
  
          public ActionResult Index()
          {
              ViewData["Message"] = "Welcome to ASP.NET MVC!";
  
              return View();
          }
  
          [HttpPost]
          public ActionResult Index(HttpPostedFileBase file)
          {
              if (file.ContentLength &gt; 0)
              {
                  var fileName = Path.GetFileName(file.FileName);
  
                  BinaryReader b = new BinaryReader(file.InputStream);
                  byte[] binData = b.ReadBytes(file.ContentLength);              
  
                  myWS.SaveImage(fileName, binData);
              }
              return RedirectToAction("Index");
          }
      }
  }
  </code></pre>
  
  <p>Maybe I am close to getting it to work? But when I run it I get a "NullReferenceException", in the top of the code where I declare the web service, there is a green line under "myWS" and I get this message from Visual Studio "Field 'HackedTwo.Controllers.HomeController.myWS' is never assigned to, and will always have its default value null". So somehow I suspect that that might be the problem why this is not working. If that is so then my question is now: <strong>How to assign the field 'HackedTwo.Controllers.HomeController.myWS'? Or should I reference the web service in some other way into my ASP.NET MVC project?</strong></p>