Heres a simlpe one. How do i get the file extension of an uploaded file or any file for that matter without using any of the substring or split functions. I thought File.PostedFile would contain a property like that but it was not to be. Thanks in advance.
As far as I know, the only way to get the extension is to parse it out...out of curiosity, why do you not want to use any of the string methods for parsing? Chris
I totally agree with the previous response but thought I would add a few other options which *may* help, depending if they are usable in your situation. (Without additional background, I'm not sure if they would apply or not.) (1) The HttpPostedFile object
does contain a ContentType property that contains the MIME type, i.e. "video/mpeg", etc. Depending on what you are trying to do, this value may be useful. Here is more information: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebHttpPostedFileClassContentTypeTopic.asp
(2) If you save the PostedFile (via the SaveAs method, using the same filename), you can then create a FileInfo object on that file. This FileInfo object contains a whole mess of useful properties, including Extension. You would have to save it to the file
system first, though, which may not be convenient in your situation. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemIOFileInfoClassTopic.asp Again, most likely the easiest thing to do is use the SubString method along
with the LastIndexOf method of the PostedFile.FileName.
Worked perfectly 'nicequy'. Thanks for the code. I would have preferred if you could do it on the PostedFile object , before storing it on the local drive. But i guess i'm asking for too much. Thanks for the help you all.
knightmare1
Member
310 Points
62 Posts
How to get file extension of uploaded file
Oct 30, 2004 05:43 PM|LINK
cahyde
Participant
1500 Points
298 Posts
Re: How to get file extension of uploaded file
Oct 30, 2004 06:33 PM|LINK
dmateer
Member
22 Points
5 Posts
Re: How to get file extension of uploaded file
Oct 30, 2004 07:09 PM|LINK
nicequy
Contributor
2652 Points
496 Posts
Re: How to get file extension of uploaded file
Oct 30, 2004 07:33 PM|LINK
MCP, MCTS, Certified SCRUM Master
asp.net blog
knightmare1
Member
310 Points
62 Posts
Re: How to get file extension of uploaded file
Oct 30, 2004 10:50 PM|LINK
nicequy
Contributor
2652 Points
496 Posts
Re: How to get file extension of uploaded file
Oct 30, 2004 11:22 PM|LINK
public class YourPage : System.Web.UI.Page{ protected System.Web.UI.HtmlControls.HtmlInputFileFile1 ;and then you can just reference it in codeLet me know how it goes!MCP, MCTS, Certified SCRUM Master
asp.net blog
vsureshvarma
Member
40 Points
21 Posts
Re: How to get file extension of uploaded file
Jun 11, 2009 11:40 AM|LINK
thankyou sir Its is working fine
raj1281.bece
Member
42 Points
7 Posts
Re: How to get file extension of uploaded file
Jun 11, 2009 11:51 AM|LINK
Hi,
You can get file extension without any split or substring. Just use in build function of io.path.getextension as below :
string strFileExtension = System.IO.Path.GetExtension(FileUploader1.PostedFile.FileName);
Rajesh Khunt
raj1281.bece
Member
42 Points
7 Posts
Re: How to get file extension of uploaded file
Jun 11, 2009 11:52 AM|LINK
Hi,
You can get file extension without any split or substring. Just use in built function of io.path.getextension as below :
string strFileExtension = System.IO.Path.GetExtension(FileUploader1.PostedFile.FileName);
Rajesh Khunt
malcolms
All-Star
18687 Points
3124 Posts
MVP
Re: How to get file extension of uploaded file
Jun 11, 2009 12:10 PM|LINK
If you want to get the extension you can use the Path class. Something like this: