Pergunta

By now I'm able to upload image by image and save it in Database , But since I've several images, I need to upload all the images contained in a folder one shot. Here is my current controller :

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

And class 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;
    }

And the view:

 @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>
               }
Foi útil?

Solução

convert HttpPostedFileBase file into a list and update your view to allow multiple uploads as follows

controller

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

Then you may need to edit your view in order to allow multiple uploads

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

as you files may be huge, DONT FORGET TO INCREASE THE REQUEST LENGTH IN YOUR WEB CONFIG FILE.

*** Edit **** There was a mistake between the input type wich should be file and the name files wich will be used for the automatic parameters by MVC

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top