Upload multiple files using HttpFileCollectionBase issue with C# and MVC3

前端 未结 3 1372
轮回少年
轮回少年 2021-01-03 05:58

I created a controller which save files.

The below code is a part of that Controller:

if ( Request.Files.Count != 0 ) {
      HttpFileCollectionBase          


        
相关标签:
3条回答
  • 2021-01-03 06:37

    Try like this:

    [HttpPost]
    public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
    {
        if (files != null && files.Count() > 0)
        {
            foreach (var uploadedFile in files)
            {
                if (uploadedFile.ContentType != "image/vnd.dwg") 
                {
                    return RedirectToAction("List");
                }
    
                var appData = Server.MapPath("~/app_data");
                var filename = Path.Combine(appData, Path.GetFileName(uploadedFile.FileName));
                uploadedFile.SaveAs(filename);                    
            }
        }
    
        return RedirectToAction("Success");
    }
    

    and modify the markup so that the file inputs are named files:

    <input type="file" name="files" />
    <input type="file" name="files" />
    ...// many inputs type file
    
    0 讨论(0)
  • 2021-01-03 06:39

    Have a look at this post by Phil Haack which demonstrates how to process multiple file uploads using MVC. The object you are trying to use is for ASP.NET Webforms.

    0 讨论(0)
  • 2021-01-03 06:42
    for (int i = 0; i < Request.Files.Count; i++)
    {
        var file = Request.Files[i];
        // this file's Type is HttpPostedFileBase which is in memory
    }
    

    HttpRequestBase.Files requires an index, so use for instead of foreach.

    0 讨论(0)
提交回复
热议问题