如何检查文件是否是MVC中的图像?

时间:2019-02-14 04:42:41

标签: c# asp.net-mvc

这是我的代码,我想检查文件是否为具有任何扩展名的图像

    [HttpPost]
    public ActionResult Add(ServicesVM obj, HttpPostedFileBase file)
    {
        if (ModelState.IsValid || file != null)
        {
            if (file != null && file.ContentLength > 0)
            {
                Services newobj = new Services();

                file = HttpContext.Request.Files[0];
                file.SaveAs(Server.MapPath("/Uploads/" + file.FileName));
                newobj.Image = "/Uploads/" + file.FileName;

                newobj.Name = obj.Name;
                newobj.Description = obj.Description;
                obj_dal.AddService(newobj);

                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Add");
            }
        }
        else
        {
            return RedirectToAction("Add");
        }
    }

0 个答案:

没有答案