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 perform a validation in Controller, assuming your method signature is something like the following:
[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
}
It is still possible to test the 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 existing.
Possibly this is from the file header. But you have two questions?
– Felipe Avelar
@Felipeavelar yes are two questions based on the same problem.
– Luiz Negrini