Home > Software engineering >  How do I upload files in several file upload fields in ASP.NET MVC?
How do I upload files in several file upload fields in ASP.NET MVC?

Time:09-06

How do I upload files in several file upload fields and send them to the controller?

Here is my code - what is wrong with it?

.cshtml file

@using (Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
        @Html.TextBox("log", "", new { type = "file"}) <br />
    </div>

    <div>
        @Html.TextBox("rpa", "", new { type = "file" }) <br />
    </div>

    <div>
        @Html.TextBox("birth", "", new { type = "file" }) <br />
    </div>

    <div>
        <input type="submit" value="Upload" />
        @ViewBag.Message
    </div>
}

Controller:

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
}

CodePudding user response:

We can use this method to get file details.

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase log, HttpPostedFileBase rpa, HttpPostedFileBase birth)
{
    if (log != null && log.ContentLength > 0)
    {
        var fileNamelog = Path.GetFileName(log.FileName);
        //write code for save file log 
    }
    if (rpa != null && rpa.ContentLength > 0)
    {
        var fileNamerpa = Path.GetFileName(rpa.FileName);
        //write code for save file rpa
    }
    if (birth != null && birth.ContentLength > 0)
    {
        var fileNamebirth = Path.GetFileName(birth.FileName);
        //write code for save file birth
    }
    return View();
}
  • Related