将文件夹中包含的所有图像上传到数据库ASPMVC4Razor

本文关键字:数据库 ASPMVC4Razor 图像 文件夹 包含 | 更新日期: 2023-09-27 18:00:57

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

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

和类ImageRender

public void UploadImageToDB(HttpPostedFileBase file, DateTime dateParution, long IdJournal, string numEditionJournal)
    {

        AnnonceVImgSet annImg = new AnnonceVImgSet();
        annImg.Id = 1;
        ImgOrgSet img = new ImgOrgSet();
        img.User_Id = 1;
        img.Journal_Id = IdJournal;
        img.dateModif = DateTime.Now;
        img.dateParution = dateParution;
        img.dateSaisi = DateTime.Now;
        img.numEditionJournal = numEditionJournal;
        img.image = ConvertToBytes(file);
        using (Bd_scanitEntities dbContext = new Bd_scanitEntities())
        {
            dbContext.ImgOrgSet.Add(img);
            dbContext.SaveChanges();
        }
    }
    public byte[] ConvertToBytes(HttpPostedFileBase Image)
    {
        byte[] image = null;
        BinaryReader reader = new BinaryReader(Image.InputStream);
        image = reader.ReadBytes((int)Image.ContentLength);
        return image;
    }

和观点:

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

将文件夹中包含的所有图像上传到数据库ASPMVC4Razor

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

控制器

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

然后你可能需要编辑你的视图,以便允许多次上传

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

因为你的文件可能很大,不要忘记增加WEB配置文件中的请求长度

***编辑***应该是file的输入类型和MVC 将用于自动参数的名称文件之间存在错误