Files Types

S

shapper

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
 
K

Kevin Spencer

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.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
M

Mark Rae [MVP]

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top