Bilal Ahmed
Bilal Ahmed

Reputation: 1129

How to get the File's Content Type in asp.net mvc 5

I know the file's name, that is saved on server, I want to get the contentType of that file that is saved on server, what I want is something like this:

string loc = Server.MapPath("~/Content/Images/document.*");

But the above code is not working and the error says :

Illegal character is specified i.e.(*)

My question is, how to get the extension and the content type of the file name that I know and is saved on server?

Upvotes: 4

Views: 9677

Answers (1)

Ali Bahrami
Ali Bahrami

Reputation: 6073

Are you looking for the file extension or the content-type (mime-type) of the file?

If you are looking for the file extension you can use Path.GetExtension:

var fileExt = Path.GetExtension(Server.MapPath("a.txt"));
// returns ".txt"

If you are looking for file mime type make use of MimeMapping.GetMimeMapping

var mimeType  = MimeMapping.GetMimeMapping("a.txt");
// returns "plain/text"

Upvotes: 9

Related Questions