Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

Files Types

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
shapper - 19 Oct 2007 22:28 GMT
Hello,

I am uploading files to a server and I need to validate the extension
of its file and get the type from it.

For example:

.doc > Microsoft Word Document

.pdf > Adobe ...

Is there a way to do this?

And if not, where can I find a list of all extensions and names given
in Windows.

Thanks,

Miguel
Kevin Spencer - 19 Oct 2007 22:48 GMT
There is no absolutely reliable way to do this. The OS stores some file
associations. Internet Explorer stores some. IIS stores some. Your best bet
is to get a list of MIME types from the web and create your own data store
of them. This can be as simple as a flat file, or stored in a database.

Signature

HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

> Hello,
>
[quoted text clipped - 15 lines]
>
> Miguel
Mark Rae [MVP] - 20 Oct 2007 00:07 GMT
> Is there a way to do this?

Following on from Kevin's reply, the following might be of some use to
you...

Supposing you have the following HTML control:
<input type="file" id="MyUpload" runat="server" />

you can add the accept tag which, depending on your browser, *might*
constrain the file patterns prompted for to match those with the
corresponding appropriate file extensions for the platform:
http://www.cs.tut.fi/~jkorpela/forms/file.html

E.g. <input type="file" id="MyUpload" runat="server"
accept="application/msword,application/pdf" />

Then, server-side, you can do the following:

// validate the file extention
if(Path.GetExtension(MyUpload.PostedFile.FileName) != ".doc" &&
Path.GetExtension(MyUpload.PostedFile.FileName) != ".pdf")
{
   // do something here
}
// validate the MIME type
if(MyUpload.PostedFile.ContentType != "application/msword" &&
MyUpload.PostedFile.ContentType != "application/pdf")
{
   // do something here
}

The <asp:FileUpload> webcontrol renders an HTML <input type="file"> control.

However, as Kevin states, this is not 100% reliable...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.