Click here to Skip to main content
14,297,742 members
Rate this:
Please Sign up or sign in to vote.
See more:

In my web page there is a file upload control. I want to check whether the uploaded file type is of .wav format only. I tried with the following code

HttpPostedFile objHPF = objHFC["fldUpload"];
string contentType = objHPF.ContentType.Split('/')[1].ToLower();

and checked whether the contenttype="wav"...

But when i uploaded the same file in two browsers(FF & Chrome) i got X-WAV in FF and WAV as content type in chrome .... So what can i do to check the content type and why this happened? Pls help.. Thanks in advance
Updated 17-Sep-13 23:35pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

All you need is C# Path.GetExtension[^]

See this also

Check type of uploaded file[^]

Arjun Menon U.K 18-Sep-13 5:41am
If i am right ,this wont get the accurate data because if i rename the file name as ABC.gif where the actual content type is png, the above code returns gif instead of png. Correct me if i am wrong
Thanks7872 18-Sep-13 5:51am
Yes.Refer to updated one.
Arjun Menon U.K 18-Sep-13 6:03am
you mean this one
static bool IsTxtFile(string f)
return f != null &&
f.EndsWith(".txt", StringComparison.Ordinal);
Arjun Menon U.K 18-Sep-13 6:10am
how can i find the content type, like if the pass the file name i must get the content type
Rate this:
Please Sign up or sign in to vote.

Solution 2

One more thing is that you can use regular expression validation control in ASP.NET to validate the extension of the file to be uploaded.

Refer this link.[^]

Use this link to get better idea to the file type[^]
Arjun Menon U.K 18-Sep-13 5:46am
The comparing data is the problem.. One browser returns wav and other x-wav for same file... so how to compare?
Thomas ktg 18-Sep-13 5:51am
use the regular expression accordingly
for example you know the file extension for different browsers. Use all the extensions in the expression. This will validate based on the extensions returned by the different browsers.
Arjun Menon U.K 18-Sep-13 5:53am
mmm that's the only way i have in mind too .. to check whether its wav/x-wav but it seems not a good logic coz god knows what else can come for a wav file next time
Thomas ktg 18-Sep-13 6:10am
Yes.Of-course it is obvious. See the updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100