我正在使用Asp.net。我试图将一些图像上传到数据库。我的问题是:fileUpload
始终为空。
这是我的创作
<div class="form-group">
@Html.LabelFor(model => model.IMG, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="fileUpload">
</div>
</div>
这是我的控制器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,NAME,PRICE,QUANTITY,DESCRIPTION,VIEWNUMBER,TIMEUPDATE,ACTIVE,CATE_DEP_ID")] PRODUCTS products)
{
HttpPostedFileBase fileUpload = Request.Files["fileUpload"];
if (ModelState.IsValid)
{
if (fileUpload != null)
{
var fileName = Path.GetFileName(fileUpload.FileName);
var path = Path.Combine(Server.MapPath("~/Content/Images/Products"), fileName);
// file is uploaded
if (System.IO.File.Exists(path))
{
ViewBag.ThongBao = "Hình ảnh đã tồn tại";
}
else
{
fileUpload.SaveAs(path);
}
products.IMG = path;
db.PRODUCTS.Add(products);
db.SaveChanges();
return RedirectToAction("Index");
}
}
ViewBag.CATE_DEP_ID = new SelectList(db.CATE_DEP, "ID", "ID", products.CATE_DEP_ID);
return View(products);
}
答案 0 :(得分:0)
请更改控制器中的以下代码行。您的代码应该可以正常工作:
HttpPostedFileBase fileUpload = Request.Files[0];