A
A
Andrey Romanyuk2017-03-28 21:49:57
ASP.NET
Andrey Romanyuk, 2017-03-28 21:49:57

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

1 answer(s)
R
Roman Zhidkov, 2017-03-29
@BLek2

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);
        }

In view:
<input type="file" name="PhotoImage" />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question