For the browser to list only image files you can do this:
<input type="file" name="file" accept="image/jpg, image/png">
However, you will also need to do a validation on the Controller, assuming the signature of your method looks something like this:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
string extensao = Path.GetExtension(file.FileName);
string[] extensoesValidas = new string[] { "jpg", "png" };
if (!extensoesValidas.Contains(extensao))
new HttpException(string.Format("Extensão de arquivo *.{0} não suportada", extensao));
var img = Image.FromStream(file.InputStream);
//código para salvar a imagem no banco
}
You can still test ContentType to ensure greater efficiency:
string[] contentTypes = new string[] { "image/jpg", "image/png" };
if (!contentTypes.Contains(file.ContentType))
{
//código para mensagem de erro
}
List of ContentTypes .