c# - 使用 asp.net mvc 上传多个文件/文件路径

标签 c# asp.net asp.net-mvc asp.net-mvc-4

我正在为房地产代理创建一个 Web 应用程序,在用于上传属性的表单之一上,我需要将多个图像或文件(例如 pdf 格式的平面图)上传到文件夹并将其路径存储在数据库中(SQL SERVER) 。

我尝试使用在线资源的一些帮助自己完成此操作,但未能解决。

这是我对文件上传的看法的一部分。

<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>

这是我处理文件上传的 Controller

[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");
        }


    }

我的主要问题是 Controller 操作的IEnumerable <HttpPostedFileBase> files中没有接收到来自 View 的文件。参数。

我希望有任何形式的指南来解决这个问题。 谢谢

最佳答案

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

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

如果您尝试使用 jquery/ajax 发帖,请查看 this excellent post关于这个话题。您基本上需要让 jquery 模仿具有该内容类型的表单帖子。

使用 HttpFileCollectionValueProvider

如果(根据您的评论/澄清),这不是您的问题,那么可能是您尝试在 Controller 级别上传这些文件的方式问题。

我一直关注this advice上传多个文件时。简而言之,尝试放弃 HttpPostedFileBase 参数或尝试将文件对象名称与参数名称匹配

如果您想坚持使用“files”作为参数名称:

<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"> 

关于c# - 使用 asp.net mvc 上传多个文件/文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35017713/

相关文章:

c# - 网络核心 : how to make the browser show file as being downloaded before it's sent

c# - 我如何更改 CSVHelper 中的字段顺序和字段名称

asp.net - 信用卡的前四个数字多久更换一次?

c# - System.Web.WebPages.TypeHelper.ObjectToDictionaryUncached

asp.net-mvc - 使用下拉列表在 mvc 3 中导航

c# - System.Data.SQLite 执行Query时出现 "No such Table"错误

c# - Microsoft fakes 的 shim 在内部是如何工作的?

c# - 如何将 xml 片段解析为节点并将它们附加到具有指定默认 namespace 的节点,以便它们成为该 namespace 的一部分?

javascript - Angularjs Kendo 主从网格应用主从的默认 ID 以添加新的详细记录

c# - 在 .net Windows 服务中处理停止