I am trying to upload a file on webpage which allow only pdf extension. But if i change the extension of any file(i.e. a.jpeg) to .pdf which will be like a.jpeg.pdf and then it can be upload on webpage. So i want to check multiple extensions in a single file before uploading it to on webpage.
Below is the my coding.
if (file != null && file.ContentLength > 0)
{
int MaxContentLength = 1024 * 1024 * 4;
string[] AllowedFileExtensions = new string[] { ".pdf" };
if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
{
ModelState.AddModelError("File", "(SERVER)Please select file of type: " + string.Join(", ", AllowedFileExtensions));
return RedirectToAction("AddNewRule", "CreateRule");
}
else if (file.ContentLength > MaxContentLength)
{
ModelState.AddModelError("File", "(SERVER)Your file is too large, maximum allowed size is: " + MaxContentLength + " MB");
return RedirectToAction("AddNewRule","CreateRule");
}
var fileName = Path.GetFileName(file.FileName);
var filetype = Path.GetExtension(fileName);
var mimeType = MimeMapping.GetMimeMapping(filetype);
if (mimeType == "application/pdf")
{
var path = Path.Combine(Server.MapPath("~/Files"), fileName);
file.SaveAs(path);
rule_add.File_Name = fileName;
rule_add.File_Path = path;
rule_add.File_Page = addrule.pagenum;
}
else
{
ModelState.AddModelError("File", "(SERVER)This file doesn't has valid content ");
return RedirectToAction("AddNewRule", "CreateRule");
}
}
Thank you for your help.