Using Two Upload controls with two buttons in single View page in ASP.Net MVC

malar
 
on Sep 03, 2021 07:15 AM
Sample_876117.zip
581 Views

Hi dharmendr,

I need two upload controls and buttons on single view.

If i place two upload controls and one button is working fine but i need separate.

For One Upload control one button and another one upload control another button.

I tried to give separate names but no luck for me.

@Html.TextBoxFor(m => m.ROIfile, new { type = "file",name="ROI", @class = "form-control" })
<button id="btnROIUpload" class="btn btn-primary" type="button" onclick="location.href='@Url.Action("ROIUpload", "ProjectUpload")'">ROIUpload</button>

@Html.TextBoxFor(m => m.file, new { type = "file"",name="DOC",, @class = "form-control" })
<button id="btnDocUpload" class="btn btn-primary" type="button">DOCUpload</button>

 

public ActionResult ROIUpload(ProjectUpload importExcel)
{
    string fileExtension = System.IO.Path.GetExtension(importExcel.file.FileName);
    string name = importExcel.ProjectCode + "-" + DateTime.Now.ToString("yyyyMMdd");
    string path = Server.MapPath("~/Content/Upload/" + "Project_Upload_" + "ROIDocuments-" + name + fileExtension);
    importExcel.file.SaveAs(path);
}

public ActionResult DOCUpload(ProjectUpload importExcel)
{
    string fileExtension = System.IO.Path.GetExtension(importExcel.file.FileName);
    string name = importExcel.ProjectCode + "-" + DateTime.Now.ToString("yyyyMMdd");
    string path = Server.MapPath("~/Content/Upload/" + "Project_Upload_" + "DOCDocuments-" + name + fileExtension);
    importExcel.file.SaveAs(path);
}

Kindly let me know where i missed.

Thanks in advance.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 03, 2021 11:55 PM

Hi malar,

Check this example. Now please take its reference and correct your code.

Model

public class ProjectUpload
{
    public string ProjectCode { get; set; }
    public HttpPostedFileBase ROIfile { get; set; }
    public HttpPostedFileBase file { get; set; }
}

Controller

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View(new ProjectUpload());
    }

    [HttpPost]
    public ActionResult Index(string submit, ProjectUpload importExcel)
    {
        switch (submit)
        {
            case "ROIUpload":
                return ROIUpload(importExcel);
            case "DOCUpload":
                return DOCUpload(importExcel);
        }
        return View();
    }

    [HttpPost]
    public ActionResult ROIUpload(ProjectUpload importExcel)
    {
        string fileExtension = System.IO.Path.GetExtension(importExcel.ROIfile.FileName);
        string name = importExcel.ProjectCode + "-" + DateTime.Now.ToString("yyyyMMdd");
        string path = Server.MapPath("~/Content/Upload/Project_Upload_ROIDocuments-" + name + fileExtension);
        importExcel.ROIfile.SaveAs(path);
        return View("Index", importExcel);
    }

    [HttpPost]
    public ActionResult DOCUpload(ProjectUpload importExcel)
    {
        string fileExtension = System.IO.Path.GetExtension(importExcel.file.FileName);
        string name = importExcel.ProjectCode + "-" + DateTime.Now.ToString("yyyyMMdd");
        string path = Server.MapPath("~/Content/Upload/Project_Upload_DOCDocuments-" + name + fileExtension);
        importExcel.file.SaveAs(path);
        return View("Index", importExcel);
    }
}

View

@model Two_File_Upload_MVC.Models.ProjectUpload

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.TextBoxFor(m => m.ProjectCode, new { name = "ProjectCode", @class = "form-control" })

        @Html.TextBoxFor(m => m.ROIfile, new { type = "file", name = "ROIfile", @class = "form-control" })
        <button id="btnROIUpload" class="btn btn-primary" type="submit" name="submit" value="ROIUpload">ROIUpload</button>

        @Html.TextBoxFor(m => m.file, new { type = "file", name = "file", @class = "form-control" })
        <button id="btnDocUpload" class="btn btn-primary" type="submit" name="submit" value="DOCUpload">DOCUpload</button>
    }
</body>
</html>