Answer the question
In order to leave comments, you need to log in
How to upload image to database in MVC framework?
Now they are developing a web application (online store) for term paper and their development. At the moment I am working on the admin panel for the site, I ran into such a problem: I don’t know how to upload an image to the database. Can you throw off some source code where it would be possible to see it visually or an intelligent video tutorial / article ..?
Answer the question
In order to leave comments, you need to log in
In controller:
public async Task<ActionResult> Create( Shop model, HttpPostedFileBase PhotoImage)
{
if (ModelState.IsValid)
{
byte[] imageData = null;
using (var binaryReader = new BinaryReader(PhotoImage.InputStream))
{
imageData = binaryReader.ReadBytes(PhotoImage.ContentLength);
}
model.Photo = imageData;
db.Shops.Add(model);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(model);
}
<input type="file" name="PhotoImage" />
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question