ユーザーがファイル アップロード コントロールから選択した画像を Content/Images などのサイト内のファイルに保存し、この画像の名前に GUID を使用して、この GUID をデータベースに保存する必要があります。私はMVCが初めてなので、詳細を教えてください。皆さん、ありがとうございました。これが私のコントローラーです...
[HttpPost]
public ActionResult Create(Product product,HttpPostedFileBase file)
{
if (file!=null&&file.ContentLength>0)
{
var FileName = string.Format("{0}.{1}",Guid.NewGuid(),file.ContentType);
var path = Path.Combine(Server.MapPath("~/Content/Images"), FileName);
file.SaveAs(path);
}
if (ModelState.IsValid)
{
db.Products.AddObject(product);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID);
return View(product);
}
そして、ここに私の見解があります...
@using (Html.BeginForm("Create", "Product", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="Uploader" />
}
何が起こったのかまったくわかりません...しかし、HttpPostedFileBaseインスタンスがないため、ifステートメントは失敗します。