Hello. Tell me how to upload a picture to the MySQL database. I added a column like MediumBLOB to the existing table in the database. They say this is a type for storing pictures. So how to upload a picture there ??? You can see the ASP ASP topic. Query a SQL C # entry in the List to make it clearer how I try to do it. not much has changed the Book model

public class Book { // ID ΠΊΠ½ΠΈΠ³ΠΈ public int Id { get; set; } // Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΠΊΠ½ΠΈΠ³ΠΈ public string Name { get; set; } // Π°Π²Ρ‚ΠΎΡ€ ΠΊΠ½ΠΈΠ³ΠΈ public string Author { get; set; } //ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° ΠΊΠ½ΠΈΠ³ΠΈ public byte[] Image { get; set; } // Ρ†Π΅Π½Π° public int Price { get; set; } } 

adding

 public byte[] Image { get; set; } 

The variant with the storage of the address of the picture will not work as I want to add a picture through the form. Tell me how it is correct

    1 answer 1

    Correctly make DTO class:

     public class BookDto { public int Id { get; set; } public string Name { get; set; } public string Author { get; set; } // Π’Π°ΡˆΡƒ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ ΠΌΠΎΠΆΠ½ΠΎ Π΄ΠΎΡΡ‚Π°Ρ‚ΡŒ Ρ‚Π°ΠΊ public HttpPostedFileBase Image { get; set;} public int Price { get; set; } } 

    A Book to do this:

     public class Book { public int Id { get; set; } public string Name { get; set; } public string Author { get; set; } // ΠŸΡƒΡ‚ΡŒ ΠΊ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ΅ public string ImageUrl { get; set; } public int Price { get; set; } } 

    Further, the method of your controller will look like this:

     public ActionResult CreateBook(BookDto bookDto) { string fileName = bookDto.Image.FileName; string path = ControllerContext.HttpContext.Server.MapPath($"~/{fileName}"); using(FileStream stream = File.Create(path)) bookDto.Image.InputStream.CopyTo(stream); Book book; // ΠΏΡ€ΠΈΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ ΠΊ Book нашСго BookDto db.Books.Add(book); db.SaveChanges(); return View(bookDto); }