Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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

C#
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
Posted
Updated 17-Sep-13 23:35pm
v2

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

See this also

Check type of uploaded file[^]

Regards..:)
 
Share this answer
 
v2
Comments
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
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.

http://www.c-sharpcorner.com/UploadFile/99bb20/validation-for-file-upload-control-in-Asp-Net/[^]

Use this link to get better idea to the file type
http://stackoverflow.com/questions/15300567/alternative-to-findmimefromdata-method-in-urlmon-dll-one-which-has-more-mime-typ[^]
 
Share this answer
 
v2
Comments
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.
ValidationExpression=/^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.wav|.WAV|.x-wav|X-WAV)$/
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900