title: Handling file upload from HTML form in ASP.net MVC 3 c#
body: <p>I have plenty of experience with this in PHP, but am fairly new to ASP.net.  I've found some tutorials around the web which have gotten me this far, but I am currently running into issues.</p>
  
  <p>So to summarize what I'm trying to accomplish, I have a page where I want users to submit contest entries.  I use a HTML form to collect things like entry title, description, and want to allow them to be able to submit an image representing their entry.</p>
  
  <p>When they choose to provide an image, I want it uploaded using a path something like:
  //entries//entry-image.</p>
  
  <p>My database will simply hold a varchar representing the image path.  I would like to add some obvious validations to the file upload as well, such as limiting the file size, and only allowing certain file extensions.  The image should be saved with some standard name (like 'entry-image' in the path above), overwriting any image that may already be in that entry directory.</p>
  
  <p>Right now getting text input from my HTML form is not a problem, but getting the file is.  I currently have no validation on the form yet, just trying to get it to work in its most basic state.  Here is a truncated version of my HTML:</p>
  
  <pre><code>&lt;form method="post" action="../Entry/Create"&gt;
              &lt;input type="text" id="EntryTitle" name="EntryTitle" /&gt;
              &lt;p id="char-remaining"&gt;(100 characters remaining)&lt;/p&gt;
  
              &lt;input type="text" id="EntryVideo" name="EntryVideo" /&gt;
              &lt;p id="vid-desc"&gt;(URL of the Video to Embed)&lt;/p&gt;
  
              &lt;input type="file" id="ImageFile" name="ImageFile" /&gt;
              &lt;p id="file-desc"&gt;(200x200px, jpeg, png, gif)&lt;/p&gt;
  
              &lt;textarea id="EntryDesc" name="EntryDesc"&gt;&lt;/textarea&gt;
              &lt;p id="entry-desc"&gt;&lt;/p&gt;
              &lt;button id="new-entry-save"&gt;save&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Now, here is my Create action in my EntryController class:</p>
  
  <pre><code>public ActionResult Create(string EntryTitle, string EntryVideo, HttpPostedFileBase ImageFile, string EntryDesc)
      {
          if (Session["User"] != null)
          {
              User currentUser = (User)Session["User"];
  
              //string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(ImageFile.FileName)); 
              //ImageFile.SaveAs(savedFileName);
  
              Entry newEntry = new Entry();
  
              newEntry.Title = EntryTitle;
              newEntry.EmbedURL = EntryVideo;
              newEntry.Description = EntryDesc;
              //newEntry.ImagePath = savedFileName;
              newEntry.UserId = currentUser.UserId;
  
              db.Entries.Add(newEntry);
              db.SaveChanges();
  
  
          }
  
          return RedirectToAction("MyPage", "User");
      }
  </code></pre>
  
  <p>You'll notice I have a few lines commented out.  When left commented out, the text input values are received just fine and stored in my database.  However, left uncommented, a NullReferenceException is thrown on that first commented-out line.</p>
  
  <p>The code I'm using in my action is pretty much just copied and pasted from a tutorial, and I realize that if it did work that it wouldn't be accomplishing the goals I wrote above since it doesn't check for file extension or change the filename to something standard.</p>
  
  <p>So my major questions are:</p>
  
  <p>1.) Why am I receiving a NullReferenceException when I am in fact providing a file on the HTML form?</p>
  
  <p>2.) How would I go about validating the file submitted? (size, extension, dimensions, etc)</p>
  
  <p>3.) How would I go about changing the name of the file to something like 'entry-image'?</p>