Share via


Content Type / File Extension ... How can I do this in ASP.NET MVC?

Question

Friday, December 12, 2008 8:54 AM

Hello,

On a form in my ASP.NET MVC Application I am uploading a file.

Before I save the file I want to validate it by checking its extension. For example:

Accept files of type jpg, gif, pdf and doc.

On my controller I have:

  HttpPostedFileBase document = Request.Files["Path"] as HttpPostedFileBase;
  string type = document.ContentType;

I think I need to use ContentType I am just not sure how to check the extension without saving the file ...

How can I do this?

Thanks,

Miguel 

All replies (1)

Friday, December 12, 2008 1:38 PM

Check HttpPostedFileBase.FileName and Path.GetExtension().