-4

HttpPostedFileBase

I want to solve this problem to store the image to the database. I still don't run the project, if I have errors or an easier way to do it

 public IActionResult Create(Productos prod [Bind("codigoFoto"] Productos productos, HttpPostedFileBase FotoProducto) 
        {
            //Console.WriteLine(JObject.FromObject(prod));
            using (var _context = new ApplicationDbContext())
            {
                var Categoria = _context.Categorias.Where(i => i.IsActive && i.Id == prod.Categoria.Id).FirstOrDefault(); 
                if (prod != null)
                {
                    if (FotoProducto != null && FotoProducto.ContentLength > 0)
                    {
                        byte[] imageData = null;

                        using (var binaryReader = new BinaryReader(FotoProducto.InputStream))
                        {
                            imageData = binaryReader.ReadBytes(FotoProducto.ContentLength);
                        }
                        //setear la imagen a la entidad que se creara
                        productos.Foto = imageData;
                    }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Luis RT
  • 7
  • 3
  • I'm voting to close this question as off-topic because it isn't written in English – TaW Apr 12 '20 at 16:18
  • Stack Overflow is an [English-only site](//meta.stackoverflow.com/a/297680). Please write posts (**including titles**) in English. – Ryan M May 05 '22 at 02:15

2 Answers2

0

In .Net Core you must use IFormFile instead of HttpPostedFileBase.

Replace

Create(Productos prod [Bind("codigoFoto"] Productos productos, HttpPostedFileBase FotoProducto)

to

Create(Productos prod [Bind("codigoFoto"] Productos productos, IFormFile FotoProducto)

this link maybe helpful for you

Farhad Zamani
  • 5,381
  • 2
  • 16
  • 41
0

Thank you very much this served me

Create(Productos prod [Bind("codigoFoto"] Productos productos, IFormFile FotoProducto)

but I also had to make a change to this code

using (var binaryReader = new BinaryReader(FotoProducto.InputStream))

var filePath = Path.GetTempFileName();
                    var file = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

                    file = Path.Combine(file, FotoProducto.FileName);


                    using (var stream = new FileStream(file, FileMode.Create))
Luis RT
  • 7
  • 3