title: Upload file and validate file extension and file size MVC 5
body: <p>I use the below code so to upload and check file extension and file size</p>
  
  <p><strong>Update 2</strong> 
  Controller     </p>
  
  <pre><code> public ActionResult Create([Bind(Include = "anak_ID,Pubdate,kind,title,file,details,link")] HttpPostedFileBase file, announcement announcement)
      {
          if (ModelState.IsValid)
          {
              db.announcement.Add(announcement);
              db.SaveChanges();
              TempData["notice"] = "Data saved";
  
              var allowedExtensions = new[] { ".pdf", ".zip", ".rar" };
  
              if (file!= null &amp;&amp; file.ContentLength &gt; 0)
              {
                  var checkextension = Path.GetExtension(file.FileName).ToLower();
  
  
                  if (itm.Contains(checkextension))
                      {
                          var extension = Path.GetExtension(file.FileName);
                          var path = Path.Combine(Server.MapPath("~/Content/AnnFiles/" + "announcement_" + announcement.anak_ID + extension));
  
                          //save File
                          file.SaveAs(path);
  
                          //prepere announcement
                          announcement.file= @"announcement_" + announcement.anak_ID + extension;
  
  
                          //Code for Save data to announcement.
  
                          db.SaveChanges();
                          TempData["notice"] = "OK! the file is uploaded";
                      }
                      else
                      {
  
                          TempData["notice"] = "Select pdf or zip or rar less than 20Μ";
  
                      }
  
              }
  
              return RedirectToAction("Create", announcement);
  
  
          }
  
          return View(announcement);
      }
  </code></pre>
  
  <p>Create view the file field.</p>
  
  <pre><code> &lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.file, htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-8"&gt;
              @Html.EditorFor(model =&gt; model.file, new { htmlAttributes = new { @class = "input-file", type = "file", name = "file"} })
  
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>Create view (the part that I display message).</p>
  
  <pre><code>  @if (TempData["notice"] != null)
      {
          &lt;div class="alert alert-danger fade in"&gt;
              &lt;a href="#" class="close" data-dismiss="alert"&gt;&amp;times;&lt;/a&gt;
              @TempData["notice"]
          &lt;/div&gt;
      }
  </code></pre>
  
  <p>It saves the record in db but in file field saves "System.Web.HttpPostedFileWrapper"</p>
  
  <p>The problem started when I changed the if statement from</p>
  
  <pre><code> if (file != null &amp;&amp; file .ContentLength &gt; 0)
  </code></pre>
  
  <p>to </p>
  
  <pre><code>if (file != null &amp;&amp; file .ContentLength &gt; 0 &amp;&amp; allowedExtensions.Contains(Path.GetExtension(file .FileName).ToLower()) &amp;&amp; file .ContentLength &lt;= (20 * 1024))
  </code></pre>
  
  <p>so to check the file extension and file size.</p>
  
  <p>Another problem is that it always displays the message 
  "Select pdf or zip or rar less than 20Μ" and saves the record. I quess because of the System.Web.HttpPostedFileWrapper value. What I want to achieve is not to save the record when I select extension that is not allowed and the file name in the table.
  Thank you in advance</p>