title: I want to upload and download file in ASP.NET MVC in database.?
body: <p>I want to upload and download files in ASP.NET MVC.</p>
  
  <p>This is my action method in the controller. When I debug file is <code>null</code>. Where am I going wrong?</p>
  
  <pre><code>public ActionResult UploadDocument(CompanyViewModel  model,HttpPostedFileBase FileUpload)
  {
      var userId = User.Identity.GetUserId&lt;int&gt;();
  
      if (model.File != null &amp;&amp; model.File.ContentLength &gt; 0)
      {
          string fileExtension = System.IO.Path.GetExtension(model.File.FileName);
  
          if (fileExtension == ".doc" || fileExtension == ".docx" || fileExtension == ".xlsx" || fileExtension == ".xls" || fileExtension == ".pdf" || fileExtension == ".jpeg" || fileExtension == ".jpg")
          {
              var file = model.File;
              var fileName = System.IO.Path.GetFileNameWithoutExtension(model.File.FileName);
              var contentLength = file.ContentLength;
              var contentType = file.ContentType;
  
              var document = new CompanyDocument();
  
              document.CreatedDate = System.DateTime.Now;
              byte[] byteData = new byte[file.InputStream.Length];
  
              file.InputStream.Read(byteData, 0, byteData.Length);
  
              document.FileData = byteData;
              document.FileName = fileName;
              document.CategoryId = model.CategoryId;
              document.ContentType = contentType;
              document.CreatedDate = DateTime.Now;
  
              _CompanyDocumentsRepository.AddOrUpdate(document);
          }
          else
          {
              TempData["ErrorMessage"] = "Only word, excel, pdf, jpg files are allowed to upload.";
              return Json(new { msgstatus = false, msg = "Only word, excel, pdf, jpg files are allowed to upload." }, JsonRequestBehavior.AllowGet);
          }
      }
  
      return Json(new { msgstatus = true, msg = "Document uploaded successfully." }, JsonRequestBehavior.AllowGet);
  }
  </code></pre>
  
  <p>My View:</p>
  
  <pre><code>@model BusSchedulingSystem.Web.ViewModels.CompanyViewModel
  
  @using(Html.BeginForm("UploadDocument","Company", null, FormMethod.Post, new { enctype="multipart/form-data"}))
  {
      if (Model != null &amp;&amp; Model.Id &gt; 0)
      {
                      @Html.HiddenFor(m =&gt; m.Id)
                  }
                  @Html.AntiForgeryToken()
  
  
                      &lt;div class="row wrapper border-bottom white-bg page-heading"&gt;
              &lt;div class="ibox float-e-margins"&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-sm-6"&gt;
                          &lt;h4 class="section-title"&gt;Upload File&lt;/h4&gt;
                          &lt;div class="ibox-content"&gt;
                              &lt;div class="form-group"&gt;
                                  @Html.LabelFor(m =&gt; m.FileName)
  
                                  &lt;input type='file' name="FileUpload" id="File"&gt;
                                  &lt;input type="submit" value="Upload" /&gt;
                                  @Html.ValidationMessageFor(model =&gt; model.FileName, "", new { @class = "text-danger" })
                              &lt;/div&gt;
  
                              &lt;div class="form-group"&gt;
  
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
  }
  </code></pre>