Diskuze: ASP.NET MVC input vs upload metoda
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Člen
Zobrazeno 7 zpráv z 7.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
zkouším něco jako toto, ale nejsme si jistej legitimitou postupu .. prosím o radu
view
<div class="form-group">
@Html.LabelFor(model => model.Picture, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*<input type="file" id="pictureFile" name="upload" />*@
@using (Html.BeginForm("UploadPicture", "Create", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="UploadPicture"/>
<input type="submit" name="UploadPicture"/>
}
@Html.ValidationMessageFor(model => model.Picture, "", new { @class = "text-danger" })
</div>
controller
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public ActionResult Create(Games games, HttpPostedFileBase uploadPicture)
{
if (ModelState.IsValid)
{
var entities = new Entities();
UploadPicture(games, uploadPicture);
entities.Games.Add(games);
entities.SaveChanges();
}
return RedirectToAction("Index");
}
public ActionResult UploadPicture(Games games, HttpPostedFileBase uploadPicture)
{
var entities = new Entities();
if (uploadPicture != null && uploadPicture.ContentLength > 0)
{
var pictureName = Path.GetFileName(uploadPicture.FileName);
string filePath = Path.Combine(Server.MapPath("~/App_Data/images"), pictureName);
uploadPicture.SaveAs(filePath);
games.Picture = Url.Content("~/App_Data/images/" + pictureName).ToString();
}
return View();
}
Koukni treba zde
http://www.mikesdotnetting.com/article/259/asp-net-mvc-5-with-ef-6-working-with-files
chápu že tehle řádek
@Html.EditorFor(model => model.Picture, new { htmlAttributes = new { @class = "form-control" } })
nabinduje to textový pole k picture z databáze
jak to ale udělat s inputem nevím a prožívám bouři errorů a vyjímek..
Snad je to srozumitelne a je to, co jsi hledal. Ostatne je to vysvetleno i v linku, ktery jsem zaslal...
You did this by specifying the type as "file" on an input element. However, this is not enough to ensure that uploaded file data is accessible on the server. You also used one of the longer overloads of the Html.BeginForm helper to add an enctype attribute to the form and sets its value to multipart/form-data. This is an essential step to getting file uploading to work.
Zkus koukout sem:
http://www.dotnetportal.cz/…roller-razor
možná ti to něco ohledně bindingu objasní
Zobrazeno 7 zpráv z 7.