I need to store the image in my database in byte[]
I am sending the image from Javascript
to mvc controller
using ajax
In my javascript
var files = $("#MyImage").get(0).files;
formData.append('files', files);
in my MVC Controller
using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
{
fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
}
Is it a correct way to store the image or I am doing this wrong?
please suggest
You can post HttpPostedFileBase
on your razor.
if (upload != null)
{
using (var inputStream = upload.InputStream)
{
var memoryStream = inputStream as MemoryStream;
if (memoryStream == null)
{
memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
}
var data = memoryStream.ToArray();
}
The method signature should be like this
[HttpPost]
public ActionResult Foo(HttpPostedFileBase upload)
{
}
And your razor side:
@using (Html.BeginForm("Foo", "ControllerName", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
}