I have a upload method within one of my controllers in my ASP.net project which works perfectly, but how would I add to restrict to file types; jpeg, jpg, png and bmp.
I've looked everywhere online and there was a lot of solutions but none of them worked for me.
Here is my code
public ActionResult Create([Bind(Include = "Id,Title,Description,FileName,FileType,FileSize,Author,DateUploaded")] FileSharing fileSharing)
{
if (Request.Files.Count > 0)
{
if (ModelState.IsValid)
{
HttpPostedFileBase file = Request.Files.Get(0);
string fileName = Path.GetFileName(file.FileName);
string filePath = Path.Combine(Server.MapPath("~/Assets/"), fileName);
file.SaveAs(filePath);
FileInfo fileInfo = new FileInfo(filePath);
fileSharing.FileType = fileInfo.Extension.Remove(0, 1).ToUpper();
fileSharing.DateUploaded = DateTime.Now;
fileSharing.FileName = fileName;
fileSharing.FileSize = fileInfo.Length.ToString();
fileSharing.Author = User.Identity.Name;
db.FileSharing.Add(fileSharing);
db.SaveChanges();
return RedirectToAction("Index");
}
}
return View(fileSharing);
}