MVC3 How to check if HttpPostedFileBase is an image

前端 未结 3 1973
野的像风
野的像风 2021-01-31 18:13

I have a controller like this:

public ActionResult Upload (int id, HttpPostedFileBase uploadFile)
{
....
}

How can I make sure that uploadFile

3条回答
  •  轮回少年
    2021-01-31 18:25

    Or you can check it on client side thru html attribute 'accept' to filter the file asap:

    @Html.TextBoxFor(x => x.HomeDeviceImage, new { @type = "file", @accept = "image/x-png, image/gif, image/jpeg" })
    

    this will only show filetypes defined in your accept attribute as default. Beware, user can still change filetye to "All files", with this in mind, better check this:

    Solved concern , a javascript snippet to check extension, and then do some editing to disable button like:

                $('input:submit').attr('disabled', true);
    

    until file extension is correct. nevertheless have it checked on server side. :)

提交回复
热议问题