使用ASP.NET MVC上传多个文件/文件路径

问题描述:

我正在为房地产经纪公司创建一个Web应用程序,并且在用于上载房地产的一种表格上,我需要将多个图像或pdf格式的平面图之类的文件上载到一个文件夹,并将其路径存储在数据库中(SQL SERVER).

I am creating a web application for an estate agency and on one of the forms used to upload properties i need to upload multiple images or files like floorplans in pdf format to a folder and store their path in the database(SQL SERVER).

我尝试使用在线资源中的一些帮助独自完成此操作,但无法对其进行整理.

I have tried doing it on my own using some help from online resources but have not been able to sort it out.

这是我上传文件视图的一部分.

This is the part of my view with the file upload.

<div class="file_inputs">

<input type="file" id="dev_brochure" name="dev_brochure" class="form-control adminarea_files">
<input type="file" id="devListingImg1" name="devListingImg1" class="form-control adminarea_files">
<input type="file" id="devListingImg2" name="devListingImg2" class="form-control adminarea_files">
<input type="file" id="devListingImg3" name="devListingImg3" class="form-control adminarea_files"> 

</div>

这是我的控制器,负责处理文件上传

This is my controller that processes the file upload

[HttpPost]
public ActionResult InsertDevelopment(development dev, IEnumerable <HttpPostedFileBase> files) 
    {
        try 
        {
          var supportedFileTypes = new [] {"jpg", "jpeg", "png", "gif", "pdf"};
            foreach(var file in files)
            {
              if(file.ContentLength > 0)
              {
                var fileExt = Path.GetExtension(file.FileName).Substring(1);
       if(!supportedFileTypes.Contains(fileExt))
           {
       TempData["Msg"] = "You have tried to upload a file type that is not allowed";
 return RedirectToAction("Create");
                    }

                 }
                }

            var devBrochure = Path.GetFileName(Request.Files["dev_brochure"].FileName);
           var devListingPhoto1_LinkName = Path.GetFileName(Request.Files["devListingImg1"].FileName);
           var devListingPhoto2_LinkName = Path.GetFileName(Request.Files["devListingImg2"].FileName);
           var devListingPhoto3_LinkName = Path.GetFileName(Request.Files["devListingImg3"].FileName);

           return View("Create");
        }
        catch(Exception e)
        {
            TempData["Msg"] = "Development upload failed :" + e.Message;
            return View("Create");
        }


    }

我的主要问题是在控制器操作的 IEnumerable< HttpPostedFileBase>中未接收到视图中的文件.文件参数.

My main problem here is that the files from the view are not being received in the controller action's IEnumerable <HttpPostedFileBase> files parameter.

我希望通过任何形式的指南来解决此问题.谢谢

I would appreciate any form of guide to fix this. Thanks

如果您只是发布表单,请确保将表单的内容类型设置为multipart/form-data.

If you are just posting a form, then make sure the content type of the form is set to multipart/form-data.

<form action="yourAction" enctype="multipart/form-data" method="post">

如果您尝试使用jquery/ajax进行发布,请查看关于主题的这篇出色的帖子.基本上,您需要使用jquery来模仿具有该内容类型的表单发布.

If you're trying to post using jquery/ajax, then check out this excellent post on the topic. You basically need to get jquery to mimic a form post with that content type.

使用HttpFileCollectionValueProvider

如果不是(根据您的评论/说明),这不是您的问题,那么这可能就是您尝试在控制器级别上载这些文件的方式.

If (based on your comment/clarification), that isn't your problem, then it could be how you are trying to upload those files at the controller level.

上传时,我一直遵循此建议多个文件.简而言之,请尝试放弃HttpPostedFileBase参数,或尝试将文件对象名称与参数名称匹配

I've always followed this advice when uploading multiple files. In short, try ditching that HttpPostedFileBase parameter OR try matching up your file object name with your parameter name

如果您要坚持使用文件"作为参数名称:

If you want to stick with "files" as the parameter name:

<input type="file" name="files[0]" id="files_0" class="form-control adminarea_files">
<input type="file" name="files[1]" id="files_1" class="form-control adminarea_files">
<input type="file" name="files[2]" id="files_2" class="form-control adminarea_files">
<input type="file" name="files[3]" id="files_3" class="form-control adminarea_files">