title: Uploading and downloading files in MVC 5
body: <p>I am new to C# and ASP.NET MVC.<br>
  I am trying to upload files and to be able to download them based on their dependencies, in Visual Studio 2013.<br>
  I have the Delegation model:</p>
  
  <pre><code>public int idDelegation { get; set; }
  public string Delegation_Name { get; set; }
  public string Employee_Name { get; set; }
  public virtual ICollection&lt;Doc&gt; Doc { get; set; }
  </code></pre>
  
  <p>and the Doc model:</p>
  
  <pre><code>public int idDoc { get; set; }
  public int idDelegation { get; set; }
  public string Doc_Name { get; set; }
  public DateTime Doc_Validity_Date { get; set; }
  public byte[] Content { get; set; }
  public virtual Delegation Delegation { get; set; }
  </code></pre>
  
  <p>What I am trying to do is upload the document related to the Delegation that it is related to.<br>
  So I have tried different ways of doing this, including saving them to server or in database.<br>
  Trying to save them in DB, I created method UploadDoc() in DocsController:  </p>
  
  <pre><code>    [HttpPost]
      public ActionResult UploadDoc(HttpPostedFileBase file)
      {
          string path = Server.MapPath(@"LocalPath\" + file.FileName);
          if (file != null &amp;&amp; file.ContentLength &gt; 0)
          {
              var fileName = Path.GetFileName(file.FileName);
              var path = Path.Combine(Server.MapPath(path), fileName);
              file.SaveAs(path);
          }
  
          byte[] uploadedFile = new byte[viewModelDoc.file.InputStream.Length];
          viewModelDoc.file.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
  
          return RedirectToAction("Index");
      }  
  </code></pre>
  
  <p>and I called it in the HttpPost Create method in DocsController:  </p>
  
  <pre><code>    [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create([Bind(Include = "idDoc,idDoc_Type,idDelegation,Doc_Name,Doc_Validity_Date, Content, FileMimeType")] Doc doc, HttpPostedFileBase file)
      {
          if (ModelState.IsValid)
          {
              UploadDoc(doc, file);
              db.Doc.Add(doc);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          ViewBag.idDelegation = new SelectList(db.Delegation, "idDelegation", "Delegation_Name", doc.idDelegation);
          ViewBag.idDoc_Type = new SelectList(db.Doc_Type, "idDoc_Type", "Doc_Type_Name", doc.idDoc_Type);
          return View(doc);
      }  
  </code></pre>
  
  <p>I created the ViewModel UploadDoc:  </p>
  
  <pre><code>public class UploadDoc
      {
          public int idDoc { get; set; }
          public string Doc_Name { get; set; }
          public DateTime Doc_Validity_Date { get; set; }
          public byte[] Content { get; set; }
          public virtual Delegation Delegation { get; set; }
          public HttpPostedFileBase file { get; set; }
       }  
  </code></pre>
  
  <p>Then, in my Create.cshtml View:  </p>
  
  <pre><code>&lt;div class="form-group"&gt;
              I
              @using (Html.BeginForm("UploadDoc", "Docs",
                          FormMethod.Post,
                          new { enctype = "multipart/form-data" }))
          {
                 &lt;div class="col-md-10"&gt;
                  @Html.TextBoxFor(x =&gt; x.file, new { type = "file" })
                  @Html.ValidationMessageFor(x=&gt;x.file)
                  &lt;button type="submit"&gt; Upload &lt;/button&gt;
                  &lt;/div&gt;
  
          }
  &lt;/div&gt;  
  </code></pre>
  
  <p>Unfortunately, the application creates the entry regarding the document(file) in the DB (like Doc_Name and other properties), but the file is not uploaded.<br>
  The other method, trying to save them on server, had the same result.<br>
  Please help. I am struggling with this problem for more than a week.<br>
  Thank you. </p>