上传多张图片并转换为base64,然后再保存到数据库

时间:2019-01-04 18:47:35

标签: c# asp.net-mvc

我有一张表格可以上传多张图片。然后,我收集这些图像,将它们上传到应用程序中的文件中,然后将它们转换为base64字符串,然后再将字符串保存到数据库中。

下面是我已经得到的:

查看:

<div class="col-xs-12 col-sm-12 col-md-6 col-lg-4 ">
    <div class="form-group">
        @Html.LabelFor(Model => Model.VueSiegeArriere, new { @class = "control-label", })
        @Html.TextBoxFor(Model => Model.VueSiegeArriere, new { @class = "form-control", type = "file", accept = "image/x-png,image/jpeg", id = "VueSiegeArriere" })
    </div>
</div>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-4 ">
    <div class="form-group">
        @Html.LabelFor(Model => Model.VueSiegeAvant, new { @class = "control-label", })
        @Html.TextBoxFor(Model => Model.VueSiegeAvant, new { @class = "form-control", type = "file", accept = "image/x-png,image/jpeg", id = "VueSiegeAvant" })
    </div>
</div>

控制器:

[HttpPost]
    public ActionResult AddVoiture(VoitureVM v, HttpPostedFileBase VueFace, HttpPostedFileBase VueArriere, HttpPostedFileBase VueGauche, HttpPostedFileBase VueDroite, HttpPostedFileBase VueSiegeArriere, HttpPostedFileBase VueSiegeAvant, HttpPostedFileBase MalleArriere, HttpPostedFileBase TableauBord)

    {
        Voiture Car = new Voiture();

        v.VueFace = UploadandConvertImage(VueFace);
        v.VueGauche = UploadandConvertImage(VueGauche);
        v.VueSiegeArriere = UploadandConvertImage(VueSiegeArriere);
        v.VueSiegeAvant = UploadandConvertImage(VueSiegeAvant);
        v.VueDroite = UploadandConvertImage(VueDroite);
        v.TableauBord = UploadandConvertImage(TableauBord);
        v.VueArriere = UploadandConvertImage(VueArriere);
        v.MalleArriere = UploadandConvertImage(MalleArriere);

        return RedirectToAction("Liste");
        //return View(v);
    }
我在控制器中调用的

UploadandConvertImage方法:

string base64String;
    private string UploadandConvertImage(HttpPostedFileBase file)
    {

        if (file != null)
        {
            string pic = System.IO.Path.GetFileName(file.FileName);
            string path = System.IO.Path.Combine(Server.MapPath("~/Pictures/Voitures"), pic);
            // file is uploaded
            file.SaveAs(path);

            //wish to convert the uploaded images to base64 and store them in database
            using (System.Drawing.Image image = System.Drawing.Image.FromFile(path))
            {
                using (MemoryStream m = new MemoryStream())
                {
                    image.Save(m, image.RawFormat);
                    byte[] imageBytes = m.ToArray();
                    base64String = Convert.ToBase64String(imageBytes);
                    //return base64String;
                }
            }
        }
        return base64String;
    }

保存到数据库:

//Let's now insert details into the table ImagesVoitures
        ImagesVoiture carImages = new ImagesVoiture();

        carImages.VueFace = voiture.VueFace;
        carImages.VueGauche = voiture.VueGauche;
        carImages.VueDroite = voiture.VueDroite;
        carImages.VueSiegeArriere = voiture.VueSiegeArriere;
        carImages.VueSiegeAvant = voiture.VueSiegeAvant;
        carImages.TableauBord = voiture.TableauBord;
        carImages.MalleArriere = voiture.MalleArriere;
        carImages.VueArriere = voiture.VueArriere;

        bdd.ImagesVoiture.Add(carImages);
        bdd.SaveChanges();

我希望得到的是base64字符串,可以将其保存到数据库中的各个字段中。

我实际上得到的是一个SQLException,它说:

  

System.Data.Entity.Validation.DbEntityValidationException:'Échecde   验证性的实体。倒加信息,   请参见'EntityValidationErrors'。

1 个答案:

答案 0 :(得分:0)

您的错误不是来自您发布的代码。在这段代码中,我也看不到您也要保存到数据库。

您的Voiture模型似乎已通过验证。您分配给car的{​​{1}}实例的字符串可能没有通过验证。

我可能会犯一个错误,但是看起来您正在发布图像,将它们转换为base64字符串,然后将其返回给客户端或视图。

我错过了什么吗?