简体   繁体   中英

Upload Multiple Files In MVC ASP.NET

I am try with this Just Upload One Files and in my case i need to upload multiple file

public ActionResult Create(HttpPostedFileBase file){
    string filename = Path.GetFileName(file.FileName);
                string contentType = file.ContentType;
                string folder = Server.MapPath("~/Files");
                using (Stream fs = file.InputStream)
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        byte[] bytes = br.ReadBytes((Int32)fs.Length);
                        file.SaveAs(Path.Combine(folder, filename));
                    }
                }
}

This My form

@using (Html.BeginForm("Create","P_m",FormMethod.Post,new { @enctype = "multipart/form-data" }))
{
<input type="file" name="File" multiple />
}

My Problem Just Uploading One File but I need to Upload Multiple File...

You can use Request.Files or through view models with property:

public IEnumerable<HttpPostedFileBase> files {get;set;}

Just replace your code with IEnumerable. If you want to use Request.Files then you should change your code structure to:

foreach (string requestFileName in Request.Files)
{
    HttpPostedFileBase file = Request.Files[requestFileName];

    string filename = Path.GetFileName(file.FileName);
    string contentType = file.ContentType;
    string folder = Server.MapPath("~/Files");
    using (Stream fs = file.InputStream)
    {
        using (BinaryReader br = new BinaryReader(fs))
        {
            byte[] bytes = br.ReadBytes((Int32)fs.Length);
            file.SaveAs(Path.Combine(folder, filename));
        }
    }
}

You Can use the below code sample for do the upload multiple file together.

Html Code:-

<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <span>Select File:</span>
            <input type="file" name="postedFiles" multiple="multiple"/>
            <hr/>
            <input type="submit" value="Upload"/>
            <br/>
            <span style="color:green">@Html.Raw(ViewBag.Message)</span>
        }
    </div>
</body>
</html>

Controller:-

ease refer my article Display (Pass) String Message from Controller to View in ASP.Net MVC.

public class UploadController : Controller
{
    // GET: Home     
    [HttpPost]
    public ActionResult Index(List<HttpPostedFileBase> postedFiles)
    {
        string path = Server.MapPath("~/Uploads/");
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        foreach (HttpPostedFileBase pFile in postedFiles)
        {
            if (postedFile != null)
            {
                string fileName = Path.GetFileName(pFile.FileName);
                pFile.SaveAs(path + fileName);
                ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", fileName);
            }
        }

        return View();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM