I have a problem, I do not know how to save more than one images to a product. For example, a product X has a Highlight image (which is what will appear in the window), and another 4 images, which will appear on the product detail page. I'm trying to save the images to a folder, and save only the path of those images in the database (Good Practices). save only one image in the folder and the URL, in the bank I can, it is not difficult, now save more than one image that I can not.
My Class Image:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace E_Commerce.Models.Loja
{
public class Imagem
{
[Key]
public int Id { get; set; }
[NotMapped]
public HttpPostedFileBase Arquivo { get; set; }
public string UrlDaImagem { get; set; }
}
}
I know that in my product I have to reference the image class. But I do not know how to save these images for the product. I searched the internet, and everything I found was examples to save only one image in the folder, and the URL in the bank also found an example of uploading multiple files, but it did not save the way in the bank. )
My Product Class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
namespace E_Commerce.Models
{
public class ProdutoTeste
{
public ProdutoTeste()
{
this.Imagens = new List<Imagem>();
}
[Key]
public int Id { get; set; }
public string Descricao { get; set; }
public decimal PrecoDeAtacado { get; set; }
public decimal PrecoDeVarejo { get; set; }
[ForeignKey("Categoria")]
public int Idcategoria { get; set; }
public virtual Categoria Categoria { get; set; }
public virtual ICollection<Imagem> Imagens { get; set; }
}
}
My Controller saves only one image, but my project has more than one image. This controller below. is an example of how to save an image in the folder, and the URL in the database. the controller for the product I have not done yet, just because I do not know how to save more than one image.
Controller:
public ActionResult Create(FotoDaColecao fotoDaColecao)
{
string nomeDoArquivo = Path.GetFileNameWithoutExtension(fotoDaColecao.Arquivo.FileName);
string extensao = Path.GetExtension(fotoDaColecao.Arquivo.FileName);
nomeDoArquivo = nomeDoArquivo + DateTime.Now.ToString("yymmssffff") + extensao;
fotoDaColecao.UrlDaImagem = "~/Imagens/Produtos/" + nomeDoArquivo;
nomeDoArquivo = Path.Combine(Server.MapPath("~/Imagens/Produtos/"), nomeDoArquivo);
fotoDaColecao.Arquivo.SaveAs(nomeDoArquivo);
db.FotosDb.Add(fotoDaColecao);
db.SaveChanges();
return RedirectToAction("Index");
}
In reality I want to do this: I'm going to register the product X, and at the moment of registering I want to select your images, to save together. Do I need to create an image controller? Someone can help !!