Stellen Sie fest, ob die hochgeladene Datei ein Bild (beliebiges Format) in MVC ist
Also benutze ich diesen Code für die Ansicht:
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" />
</form>
Dies für modell:
[HttpPost]
public ActionResult Index(HttpPostedFileBase file) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
return RedirectToAction("Index");
}
Funktioniert hervorragend, wenn der Benutzer keine Datei hinzufügt, die kein Bild ist. Wie kann ich sicherstellen, dass die hochgeladene Datei ein Bild ist? Vielen Dank