Я намерен сохранить файл изображения на SQL сервере, но не могу получить строковое значение объекта.
ProductController
:
[HttpPost]
public ActionResult Create([Bind(Include = "pro_id,pro_name,pro_cat,pro_quantity,pro_unitPrice,pro_barImage")] product product, HttpPostedFileBase ImageFile)
{
if (ModelState.IsValid)
{
string fileName = Path.GetFileNameWithoutExtension(product.ImageFile.FileName);
string extension = Path.GetExtension(product.ImageFile.FileName);
fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
product.pro_barImage = "~/image1/" + fileName;
fileName = Path.Combine(Server.MapPath("~/image1/"), fileName);
product.ImageFile.SaveAs(fileName);
db.products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.pro_cat = new SelectList(db.categories, "cat_id", "cat_name",product.pro_cat);
ModelState.Clear();
return View(product);
}
Create.cs html:
<div class="form-group">
@Html.LabelFor(model => model.pro_barImage, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ActionLink("GenerateBarCode", "Index", new { Area = "BarCode", Controller = "GenerateBarCode" }, new { @Parameter = "value" })
<img src="@ViewBag.BarcodeImage" />
</div>
<br />
<div class="col-md-10">
<input type="file" id="ImageFile" name="ImageFile" required />
</div>
<br />
</div>
Проблема, с которой я столкнулся:
Сущность (Product
таблица):
![Product Table](https://i.stack.imgur.com/U8kzR.png)
Product.cs: product.cs