Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 使用MVC将多个图像保存到数据库_Asp.net Mvc_Entity Framework - Fatal编程技术网

Asp.net mvc 使用MVC将多个图像保存到数据库

Asp.net mvc 使用MVC将多个图像保存到数据库,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我的代码只保存第一个输入图像。我发现我可以处理多个图像。如果我将HttpPostedFileBase更改为HttpPostedFileBase[],我将无法对HttpPostedFileBase图像使用InputStream和ContentLength [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "Id,Name,Color,CategoryId,GenderId,Image")

我的代码只保存第一个输入图像。我发现我可以处理多个图像。如果我将
HttpPostedFileBase
更改为
HttpPostedFileBase[]
,我将无法对
HttpPostedFileBase图像使用
InputStream
ContentLength

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,Color,CategoryId,GenderId,Image")] Product product, HttpPostedFileBase image)
    {
        if (ModelState.IsValid)
        {
            byte[] imagee = null;
            if (Request.Files.Count > 0)
            {
                image = Request.Files[0];
                using (BinaryReader br = new BinaryReader(image.InputStream))
                {
                    imagee = br.ReadBytes(image.ContentLength);
                }
            }
            product.Image = imagee;
            db.Products.Add(product);
            db.SaveChanges();
            return RedirectToAction("Index");
    }
我只展示了一张这样的图片:

<img class="img-rounded img-responsive" src="data:image/jpg;base64,@System.Convert.ToBase64String(item.Image)" width="250" height="400" />


如何存储多个图像并同时显示它们

正如Stephen在评论中提到的,您当前的实体类定义用于存储针对产品的单个图像(一对一)。如果你喜欢对图像有多个图像,你应该考虑为图像创建另一个表,并在它们之间建立一对多的连接。
public class Product
{
  public int Id { set; get;}
  public string Name { set; get;}
  public string Color { set; get;}
  // Add other properties needed as well

  public ICollection<Image> Images { set; get;}
}
public class Image
{
  public int Id { set; get;}
  public byte[] ImageData { set; get;}
  public int ProductId { set; get;}
}
公共类产品
{
公共int Id{set;get;}
公共字符串名称{set;get;}
公共字符串颜色{set;get;}
//还需要添加其他属性
公共ICollection映像{set;get;}
}
公众阶级形象
{
公共int Id{set;get;}
公共字节[]图像数据{set;get;}
public int ProductId{set;get;}
}
现在在您的HttpPost操作中

[HttpPost]
public ActionResult Create([Bind(Include = "Id,Name,Color,CategoryId,GenderId")]
                          Product product, IEnumerable<HttpPostedFileBase> images)
{
     if (ModelState.IsValid)
     {
         db.Products.Add(product);

         if (images!=null)
         {
            var imageList= new List<Image>();
            foreach(var image in images)
            {
               using (var br = new BinaryReader(image.InputStream))
               {
                 var data = br.ReadBytes(image.ContentLength);
                 var img=new Image { ProductId=product.Id };
                 img.ImageData = data;
                 imageList.Add(img);
               }
            }
            product.Images = imageList;
         }
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(product);
}
[HttpPost]
公共操作结果创建([Bind(Include=“Id,Name,Color,CategoryId,GenderId”)]
产品,IEnumerable图像)
{
if(ModelState.IsValid)
{
db.Products.Add(产品);
如果(图像!=null)
{
var imageList=新列表();
foreach(图像中的var图像)
{
使用(var br=新的二进制读取器(image.InputStream))
{
var data=br.ReadBytes(image.ContentLength);
var img=新映像{ProductId=product.Id};
img.ImageData=数据;
imageList.Add(img);
}
}
product.Images=imageList;
}
db.SaveChanges();
返回操作(“索引”);
}
返回视图(产品);
}

您不能使用InputStream和ContentLength是什么意思?为什么要使用
Request.Files[0]
-您的方法已经有了文件的参数。如果它的
IEnumerable image
,那么您只需在文件中循环-
foreach(图像中的HtttpPostedFileBase文件){..
,也就是说,它显示了“InputStream”的
定义,并且没有扩展方法“InputStream”接受类型为“HttpPostedFileBase[]的第一个参数“
ContentLength
的相同错误。我们的教授共享了此代码,但我想将代码扩展为存储多个图像。根据您的评论,我是否只使用
foreach
获取每个输入?
HttpPostedFileBase[]
是一个集合-您需要循环遍历集合以获取其中的每个文件。当然,您的
产品。图像
不起作用,因为它只包含一个图像。您需要第二个数据库表来存储图像(产品ID带有FK),这样新模型将具有ID主键、ProductId外键和字节[]中的图像。当我选择多个图像时,程序将使用新创建的模型存储它们,我将从新创建的模型中获取图像以显示它们。我是对的,还是您可以显示一个示例?