So möchte ich den Benutzer meiner Website Bilder hochladen können. Ich möchte, dass die Bilder im Solution Explorer in einem Ordner namens Images hochgeladen werden und der Dateiname in einer Datenbank gespeichert wird.Bild wird nicht in Lösungsexplorer hochgeladen
Ansicht erstellen:
@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>
Controller:
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);
}
I codiert es in einer Weise, dass der Benutzer nicht Bild im Artikel einzufügen wählen könnte. Bis jetzt speichert mein Code den Dateinamen erfolgreich in der Datenbank als Zeichenfolge, aber das Bild selbst wird nicht im Lösungsexplorer hochgeladen.
Vielleicht habe ich verwirrt, sollte der "Lösungsexplorer", den du meinst, der Image-Ordner im Deploy-Pfad sein? Hast du das überprüft? – Prisoner
Der Ordner "Images" (den ich erstellt habe) im Solution Explorer. Ja, ich habe das überprüft. – PaginationIsLife
Warten Sie eine Minute, der 'Images'-Ordner, den ich sagte, befindet sich im Bereitstellungs-Pfad, nicht der, den Sie erstellt haben, und der im Lösungs-Explorer. Wenn Sie IIS verwenden, sollte es sich irgendwo in 'wwwroot' befinden, was Sie im Lösungsexplorer nicht sehen können (außer Sie fügen das manuell hinzu ...), sprechen wir den gleichen Ordner? – Prisoner