私のウェブサイトのユーザーが画像をアップロードできるようにします。 Imageという名前のフォルダにあるソリューションエクスプローラに画像をアップロードし、そのファイル名をデータベースに保存します。画像がソリューションエクスプローラでアップロードされない
は、ビューの作成:
@model OnlineShop.Models.Items
@{
ViewBag.Title = "Create";
}
<h2>Edit</h2>
@using (Html.BeginForm("Create", "AdminItems", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Account</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div>
Name
</div>
<div class="col-md-10">
@Html.EditorFor(model => model.Item_Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Item_Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div>
Details
</div>
<div class="col-md-10">
@Html.EditorFor(model => model.Item_Details, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Item_Details, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div>
Stock
</div>
<div class="col-md-10">
@Html.EditorFor(model => model.Item_Stock, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Item_Stock, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div>
Category
</div>
<div class="col-md-10">
@Html.EditorFor(model => model.Item_Category, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Item_Category, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div>
Account Id
</div>
<div class="col-md-10">
@Html.EditorFor(model => model.Account_Id, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Account_Id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div>
Cost
</div>
<div class="col-md-10">
@Html.EditorFor(model => model.Item_Cost, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Item_Cost, "", new { @class = "text-danger" })
</div>
</div>
<fieldset>
<label for="file">Upload Image:</label>
<input type="file" name="file" id="file" style="width: 100%;" />
</fieldset>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
コントローラー:
public ActionResult Create([Bind(Include = "Item_Name, Item_Details, Item_Stock, Item_Cost, Account_Id, Item_Category")]Items item, HttpPostedFileBase file)
{
OnlineShopDbContext db = new OnlineShopDbContext();
item.Item_Date = DateTime.Now;
try
{
if (ModelState.IsValid)
{
if (file != null)
{
string ImageName = System.IO.Path.GetFileName(file.FileName);
string physicalPath = Server.MapPath("~/Images/" + ImageName);
file.SaveAs(physicalPath);
item.Item_ImagePath = ImageName;
}
db.Items.Add(item);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (RetryLimitExceededException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.)
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
return View(item);
}
を、私は、ユーザーが項目に画像を挿入しないことを選択することができること方法でそれをコード化されました。 これまでのところ、私のコードは文字列としてデータベースにファイル名を正常に格納しましたが、イメージ自体はソリューションエクスプローラにアップロードされません。
「ソリューションエクスプローラ」は、展開パスの 'image'フォルダを意味するのではないでしょうか?あなたはそれをチェックしましたか? – Prisoner
ソリューションエクスプローラで(作成した)「Images」フォルダ。はい、私はそれを確認しました。 – PaginationIsLife
私が言った 'Images'フォルダは、あなたが作成したものとソリューションエクスプローラのものではなく、展開パスにあります。 IISを使用している場合は、 'wwwroot'のどこかにあるはずです。ソリューションエクスプローラで見ることができないはずです(手動で追加する場合を除いて...)。 – Prisoner