title: ASP.NET MVC File Upload Error - "The input is not a valid Base-64 string"
body: <p>I'm trying to add a file upload control to my ASP.NET MVC 2 form but after I select a jpg and click Save, it gives the following error:</p>
  
  <p><strong>The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.</strong></p>
  
  <p>Here's the view:</p>
  
  <pre><code>&lt;% using (Html.BeginForm("Save", "Developers", FormMethod.Post, new {enctype = "multipart/form-data"})) { %&gt;
      &lt;%: Html.ValidationSummary(true) %&gt;
      &lt;fieldset&gt;
          &lt;legend&gt;Fields&lt;/legend&gt;
  
          &lt;div class="editor-label"&gt;
              Login Name
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              &lt;%: Html.TextBoxFor(model =&gt; model.LoginName) %&gt;
              &lt;%: Html.ValidationMessageFor(model =&gt; model.LoginName) %&gt;
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              Password
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              &lt;%: Html.Password("Password") %&gt;
              &lt;%: Html.ValidationMessageFor(model =&gt; model.Password) %&gt;
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              First Name
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              &lt;%: Html.TextBoxFor(model =&gt; model.FirstName) %&gt;
              &lt;%: Html.ValidationMessageFor(model =&gt; model.FirstName) %&gt;
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              Last Name
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              &lt;%: Html.TextBoxFor(model =&gt; model.LastName) %&gt;
              &lt;%: Html.ValidationMessageFor(model =&gt; model.LastName) %&gt;
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              Photo
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              &lt;input id="Photo" name="Photo" type="file" /&gt;
          &lt;/div&gt;
  
          &lt;p&gt;
              &lt;%: Html.Hidden("DeveloperID") %&gt;
              &lt;%: Html.Hidden("CreateDate") %&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  &lt;% } %&gt;
  </code></pre>
  
  <p>And the controller:</p>
  
  <pre><code>//POST: /Secure/Developers/Save/
          [AcceptVerbs(HttpVerbs.Post)]
          public ActionResult Save(Developer developer)
          {
              //get profile photo.
              var upload = Request.Files["Photo"];
              if (upload.ContentLength &gt; 0)
              {
                  string savedFileName = Path.Combine(
                        ConfigurationManager.AppSettings["FileUploadDirectory"],
                        "Developer_" + developer.FirstName + "_" + developer.LastName + ".jpg");
                  upload.SaveAs(savedFileName);
              }
              developer.UpdateDate = DateTime.Now;
              if (developer.DeveloperID == 0)
              {//inserting new developer.
                  DataContext.DeveloperData.Insert(developer);
              }
              else
              {//attaching existing developer.
                  DataContext.DeveloperData.Attach(developer);
              }
              //save changes.
              DataContext.SaveChanges();
              //redirect to developer list.
              return RedirectToAction("Index");
          }
  </code></pre>
  
  <p>Thanks,
  Justin</p>