Firstly, in your .cshtml file use a file controler
@using (Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
@Html.AntiForgeryToken()
<div class="form-group">
<input type="file" id="fileToUpload" name="file" />
<span class="field-validation-error" id="spanfile"></span>
</div>
<div class="form-group">
<input type="submit" value="Upload" class="btn btn-info" id="UploadFile" name="action" />
</div>
}
now in your controller validate the file, get the byte array and store it n the db.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> UploadFile(HttpPostedFileBase file, CustomModel model)
{
if (ModelState.IsValid)
{
if (file == null)
{
ModelState.AddModelError(string.Empty, "please upload a file");
}
else if (file.ContentLength > 0)
{
// check if its a valid image type
// get the byte array
byte[] byteArray = file.ToByteArray();
// Store this byte array in db
}
}
return View(model);
}