C# 将文件夹中包含的所有图像上载到数据库ASP MVC 4

C# 将文件夹中包含的所有图像上载到数据库ASP MVC 4,c#,asp.net-mvc-4,razor,image-uploading,C#,Asp.net Mvc 4,Razor,Image Uploading,现在我可以一张一张地上传图片并保存在数据库中,但由于我有多张图片,我需要一次性上传文件夹中包含的所有图片。 这是我当前的控制器: [HttpPost] public ActionResult Form(HttpPostedFileBase file, DateTime dateParution, long IdJournal, string numEditionJournal) { var db = new Bd_scanitEntities();

现在我可以一张一张地上传图片并保存在数据库中,但由于我有多张图片,我需要一次性上传文件夹中包含的所有图片。 这是我当前的控制器:

 [HttpPost]
    public ActionResult Form(HttpPostedFileBase file, DateTime dateParution, long IdJournal, string numEditionJournal)
    {
        var db = new Bd_scanitEntities();
        IEnumerable<SelectListItem> items = db.JournalSet
          .Select(c => new SelectListItem
          {
              Value = c.Id.ToString(),
              Text = c.label
          }).OrderBy(c => c.Text);/*_*/
        ViewBag.IdJournal = items;

        ScanITAPP.Service.ImageRender service = new Service.ImageRender();
        service.UploadImageToDB(file, dateParution, IdJournal, numEditionJournal);

        return RedirectToAction("Index");
    }
以及以下观点:

 @using (Html.BeginForm("Form", "Home", FormMethod.Post, new { @enctype = "multipart/form-data" }))
               {     
<div class="modal-dialog">
           <div class="modal-content">
                <div class="modal-header">
                     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                     <h4 id="myModalAlertLabel" class="modal-title">Import</h4>
                </div>
                <div class="modal-body">

                        <table cellpadding="2" cellspacing="10">
                            <tr>
                                <td>
                                    Choisir journal :
                                </td>
                                <td>
                                    @Html.DropDownList("IdJournal")
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Numéro de l'édition:
                                </td>
                                <td>
                                    <input type="text" name="numEditionJournal" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Date de parution:
                                </td>
                                <td>
                                    <input class="form-control span2" name="dateParution" size="16" type="date" value="12-02-2014" >


                            </tr>
                            <tr>
                                <td>
                                    Choisirr image:
                                </td>
                                <td>
                                    <input type="file" name="file" id="upload"/>
                                </td>
                            </tr>
                        </table>


                </div>
                <div class="modal-footer">

                     <button type="submit" id="load" class="btn btn-primary">Confirmer</button>
                </div>
          </div>
    </div>
               }
@使用(Html.BeginForm(“Form”,“Home”,FormMethod.Post,new{@enctype=“multipart/formdata”}))
{     
&时代;
进口
Choisir杂志:
@Html.DropDownList(“IdJournal”)
修订编号:
谈判日期:
唱诗班图像:
证实人
}

HttpPostedFileBase文件
转换为一个列表,并更新视图,以允许多次上传,如下所示

控制器

   [HttpPost]
public ActionResult Form(List<HttpPostedFileBase> files, DateTime dateParution, long IdJournal, string numEditionJournal)
{
    var db = new Bd_scanitEntities();
    IEnumerable<SelectListItem> items = db.JournalSet
      .Select(c => new SelectListItem
      {
          Value = c.Id.ToString(),
          Text = c.label
      }).OrderBy(c => c.Text);/*_*/
    ViewBag.IdJournal = items;

    ScanITAPP.Service.ImageRender service = new Service.ImageRender();
    foreach(HttpPostedFileBase file in files){
    service.UploadImageToDB(file, dateParution, IdJournal, numEditionJournal);
   }

    return RedirectToAction("Index");
}
[HttpPost]
公共操作结果表单(列表文件、DateTime dateParution、长IdJournal、字符串NuMediationJournal)
{
var db=新的Bd_扫描实体();
IEnumerable items=db.JournalSet
.选择(c=>new SelectListItem
{
Value=c.Id.ToString(),
Text=c.标签
}).OrderBy(c=>c.Text)/*_*/
ViewBag.IdJournal=项目;
ScanITAPP.Service.ImageRender服务=新服务.ImageRender();
foreach(文件中的HttpPostedFileBase文件){
UploadImageToDB(文件、日期解析、IdJournal、NuMediationJournal);
}
返回操作(“索引”);
}
然后,您可能需要编辑视图以允许多次上载

  .... <input type="file" multiple="multiple" name="files" id="upload"/>
。。。。
因为你的文件可能很大, 别忘了增加WEB配置文件中的请求长度。

***编辑****MVC自动参数的输入类型应为file,而名称文件应为file之间存在错误

  .... <input type="file" multiple="multiple" name="files" id="upload"/>