What is the best way to save multiple image links in a database?

Asked

Viewed 49 times

0

List<String> _lista = new List<String>();
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Veiculo veiculo, HttpPostedFileBase[] file)
    {

            for (int i = 0; i < file.Length; i++)
            {
                var fileName = Guid.NewGuid().ToString() +
                 System.IO.Path.GetExtension(file[i].FileName);
                file[i].SaveAs(HttpContext.Server.MapPath("~/Fotos/")
                                                  + fileName);
                _lista.Add(fileName);
            }
            veiculo.Imagem1 = _lista[0];
            veiculo.Imagem2 = _lista[1];
            veiculo.Imagem3 = _lista[2];
            veiculo.Imagem4 = _lista[3];
            veiculo.Imagem5 = _lista[4];
            db.Veiculo.Add(veiculo);
            db.SaveChanges();
            return RedirectToAction("Index");

    }

What I need is that the user can upload as many images as he wants and not only 5 as and done in my code.

1 answer

2


The ideal in this case, according to data normalization, is to separate this information in another table in a 1xN relationship.

For example, create the table veiculo_imagens with the fields id_veiculo_imagens (primary key), id_veiculo (foreign key) and imagem.

With this, 1 vehicle may have n images.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.