简体   繁体   English

使用ASP.NET MVC中的Entity Framework将多张图片上传到SQL服务器数据库

[英]Upload multiple pictures to SQL Server database using Entity Framework in ASP.NET MVC

I have been trying to upload multiple pictures to a SQL Server database by using Entity Framework.我一直在尝试使用实体框架将多张图片上传到 SQL 服务器数据库。

I have successfully uploaded one picture to the database before but I need to upload multiple pictures one time.我之前已经成功将一张图片上传到数据库,但我需要一次上传多张图片。

I tried to take the pictures as ICollection<IFormFile> Pic in the DtoModel and convert them to byte[] in the primary model to store them in the database but it didn't work and I just stored one picture in the database from many I have uploaded.我尝试将图片作为 DtoModel 中的ICollection<IFormFile> Pic并将它们转换为主要 model 中的 byte[] 以将它们存储在数据库中,但它不起作用,我只是将一张图片从许多我存储在数据库中已上传。

primary model:初级 model:

public class property
{
    [Key]
    public int Id { get; set; }
   
    public string Name { get; set; }

    [Required(ErrorMessage = "Photo is required.")]
    public byte[] Pic { get; set; }

    public string PicFromat { get; set; }
}

DtoModel:模型:

public class dtoprop
{
    public string Name { get; set; }

    public ICollection<IFormFile> Pic { get; set; }

    public string PicFromat { get; set; }

}

Controller: Controller:

private readonly ApplicationDbContext _context;
private new List<string> _allowedExtenstions = new List<string> { ".jpg", ".png" }; 
private long _maxAllowedPosterSize = 1048576;

public propertiesController(ApplicationDbContext context)
{
    _context = context;
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create( dtoprop model)
{
        if (!ModelState.IsValid)
        {
            return View( model);
        }

        if(!_allowedExtenstions.Contains(Path.GetExtension(model.Pics.FileName.ToLower())))
        {
            ModelState.AddModelError("Pic", "Only .PNG, .JPG images are allowed!");
    
            return View(model);
        }
    
        if (model.Pics.Length > _maxAllowedPosterSize)
        {
            ModelState.AddModelError("Pic", "Poster cannot be more than 1 MB!");
            return View(model);
        }

        using var dataStream = new MemoryStream();

        await model.Pics.CopyToAsync(dataStream);

   using var dataStream = new MemoryStream();
            byte[] conv = null;


            if (model.Pic != null )
            {
                // Loop thru each selected file
                foreach (IFormFile photo in model.Pic)
                {
                    await photo.CopyToAsync(dataStream);

                     conv = dataStream.ToArray();

                }
            }


        var pic = new property
        {
            Name = model.Name,
             Pic = conv,
            PicFromat = model.PicFromat
        };

        _context.Properties.Add(pic);
        _context.SaveChanges();

        return RedirectToAction("Index");
}

So, do I need to create a new image model with a one-to-many relationship with the primary model?那么,我是否需要创建一个与主 model 具有一对多关系的新映像 model? or there is a better way can implement this approach without store the images in the wwwroot folder?或者有更好的方法可以实现这种方法而不将图像存储在 wwwroot 文件夹中?

rewrite your controller code block as follows:重写您的 controller 代码块,如下所示:

if (model.Pic != null )
{
    // Loop thru each selected file
    foreach (IFormFile photo in model.Pic)
    {
        var dataStream = new MemoryStream();
        await photo.CopyToAsync(dataStream);

        byte[] conv = dataStream.ToArray();

        var pic = new property()
        {
            Name = model.Name,
            Pic = conv,
            PicFromat = model.PicFromat
        };

        _context.Properties.Add(pic);
    }
}

_context.SaveChanges();

Here are few things you need to consider for such approach ie对于这种方法,您需要考虑以下几点,即

Make sure that the underlying database eg in case of SQL server must have data type as VARBINARY(MAX) .确保基础数据库,例如 SQL 服务器的数据类型必须为VARBINARY(MAX) this will store the byte array data in a single row.这会将字节数组数据存储在一行中。

Seeing your code I can tell that you actually needs multi-row List<byte[]> data type to store the multiple images bytes inside a single row of table.看到您的代码,我可以告诉您实际上需要多行 List<byte[]> 数据类型来将多个图像字节存储在单行表中。

Since you are just using the datatype byte[] it means that in your foreach loop the last image you convert to byte array will be stored in your variable, meaning in your conv variable you are storing the bytes of single image not multiple images.由于您只是使用数据类型 byte[] 这意味着在您的foreach 循环中,您转换为字节数组的最后一个图像将存储在您的变量中,这意味着在您的conv变量中,您存储的是单个图像的字节而不是多个图像。

Know that any database infrastructure does not allows to store list of anything even byte array in a single row, you eventually need to use one to may database relation to store the list of multiple files byte array in a multiple rows separately .知道任何数据库基础设施都不允许将任何列表甚至字节数组存储在单行中,您最终需要使用一个可能的数据库关系将多个文件字节数组的列表分别存储在多行中

You may have found some hack, but it eventually runs to data type max limits as either your number of images increases or your image size increases.您可能已经发现了一些技巧,但随着图像数量的增加或图像大小的增加,它最终会达到数据类型的最大限制。

In short, you need one to many relation mapping model to store multiple images even in bytes in the database, you can not store multiple images byte array in a single row by design.简而言之,您需要一对多的关系映射 model 以在数据库中以字节存储多个图像,您不能通过设计将多个图像字节数组存储在单行中。 I hope I am able to clarify few things here.我希望我能够在这里澄清一些事情。

It sounds like you would need to create a separate table for the Image Name "test1" and the unique ID (primary key) which will be used to locate the collection of images uploaded to separate table.听起来您需要为图像名称“test1”和唯一 ID(主键)创建一个单独的表,该 ID 将用于定位上传到单独表的图像集合。

Upload would require two writes, one to the table that stores the "ImageName" and unique key and second write to upload the images individually with a column linking them back to the "ImageName".上传需要两次写入,一次写入存储“ImageName”和唯一键的表,第二次写入单独上传图像,并使用将它们链接回“ImageName”的列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ASP.NET MVC中的Entity Framework将图片上传到SQL服务器数据库 - Upload picture to SQL Server database using Entity Framework in ASP.NET MVC 如何使用ASP.NET MVC中的实体框架在SQL Server中保存业务模型实体 - How to save a business model entity in SQL Server using Entity Framework in ASP.NET MVC ASP.NET MVC with ENTITY FRAMEWORK (SQL SERVER DATABASE) 图像未显示在视图中......错误显示“无法将“字节”转换为“字符串”” - ASP.NET MVC with ENTITY FRAMEWORK (SQL SERVER DATABASE) Image not showing in view ... The error says 'cannot convert "byte" to "string"' C#如何使用ASP.NET MVC中的实体框架将父数据和子数据插入/更新到sql数据库 - C# How to insert/update the parent and child data to the sql database using entity framework in asp.net mvc 如何使用带有Entity Framework的ASP.net MVC4上传/显示图像 - How to upload/display images using ASP.net MVC4 with Entity Framework 努力使用实体框架将数据保存到ASP.NET MVC中的数据库 - Struggling with saving data to database in ASP.NET MVC with Entity Framework 没有实体框架数据库连接的ASP.NET MVC - ASP.NET MVC without Entity Framework database connection PopUp 不在 ASP.NET MVC 5 和实体框架中的数据库中发送数据 - PopUp not sending data in database in ASP.NET MVC 5 and Entity Framework ASP.net MVC与Entity Framework重新排序数据库记录 - ASP.net MVC with Entity Framework reordering database records ASP.NET MVC 4实体框架数据库第一个存储库 - ASP.NET MVC 4 Entity Framework Database First Repository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM