Ich habe versucht, mein Bestes jedoch HttpPostedFileBase filee
ist immer null
Datei-Upload ASP.NET MVC funktioniert nicht
Controller Aktion
public ActionResult UploadFile(HttpPostedFileBase filee)
{
try
{
if (filee.ContentLength > 0)
{
string _FileName = Path.GetFileName(filee.FileName);
string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);
filee.SaveAs(_path);
}
ViewBag.Message = "File Uploaded Successfully!!";
return View();
}
catch
{
ViewBag.Message = "File upload failed!!";
return View();
}
}
Razor Ansicht
@{
ViewBag.Title = "UploadFile";
}
<h2>UploadFile</h2>
@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
@Html.TextBox("file", "", new { type = "file" }) <br />
<input type="submit" value="Upload" />
@ViewBag.Message
</div>
}
Da der Name der Datei eingegeben wird 'file' nicht' filee'. Sie müssen übereinstimmen. –
Werfen Sie einen Blick auf [Diese Antwort] (https://stackoverflow.com/a/8551621/3814721) – mmushtaq
Jeder gute Grund? dafür. Wir haben immer die Wahl, die Instanz nach unserem Wunsch zu benennen. Mitarbeiter Mitarbeiter = neuer Mitarbeiter(); –