Upload File. File Type Question.

S

Shapper

Hello,

I created a script to upload a file.
To determine the file type I am using userPostedFile.ContentType.

For example, for a png image I get "image/png".

My questions are:
1. Where can I find a list of all the types which can be returned?
For example, if it is a Word document will it return "document/doc".
I would like to see a list of it. I can't find it anywhere.

2. How can I get File Type and File Extension in two different
variables?
For "image/png" I would like to get:
FileType = "image"
FileExtension = "png"

Thank You,
Miguel
 
S

S. Justin Gengo

Shapper,

While I'm not positive of where to find a list of all file types (I'd google
for them myself) splitting the type and extension is pretty easy just use
the string object's split function.

Dim FileArray() As String = FileType.Split("\")

Now the string array FileArray will contain two values: FileArray(0) =
"image" and FileArray(1) = "png"

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S

Shapper

Hi Justin,

Thanks.

I Googled it and I couldn't find any information about it.
I thought I would get that information on MSDN under
userPostedFile.ContentType but I had no success.

Does anyone else knows where I can I find this information?

Thanks,
Miguel
 
K

Kevin Spencer

You're really approaching this from the wrong angle. The browser doesn't
know what the file type is. It only knows what the file extension is. It
then makes its best guess as to the MIME type of the file. There are not
MIME types for every file extension. Therefore, you're not necessarily going
to get information about the type of file via the MIME type. Instead, I
recommend that you do the following:

1. Identify exactly the requirement regarding what type of file is being
uploaded. You're not able to determine the "type" of just any file uploaded.
Ever looked through your local file system in Windows Explorer? It has a
column called "Type" which shows a friendly name for the "file type" of the
files in the system. In most cases, it displays a friendly name for the file
type. However, in some cases, you'll simply see the file extension, as in
"EXT File." This happens when Windows does not have a registration for that
file's extension. Therefore, you need to determine the reason that your app
seems to need to identify the "file type" of the file being uploaded. It is
not too difficult to identify a text file versus a binary file, for example.
But you haven't mentioned the reason you need to identify the "file type,"
and that will determine what your app needs to do to identify it to the
extent that you need to.

2. Use the file extension, rather than the MIME type for the file. As I
mentioned before, there are far fewer MIME types than there are file
extensions.

There are web sites that identify the types of programs that work with
various file extensions. You might want to Google them, and perhaps create a
database of file extensions mapped to "type identifiers" for the file
extensions. In general, the "file type" of a given file is identified as the
program or programs which interact with files having that extension. Many
file extensions are associated with multiple programs, and have a more
generic "file type name" applied to them, such as ".txt" files.

As an alternative, in a recent project, I built an Explorer-like interface
for working with files in a machine's local file system. I wanted it to look
like Explorer, so I wanted to include the "Type" column in the interface.
Upon researching how Explorer identifies the "file type" of a file, I found
out that it uses the System Registry, where file extensions are associated
with applications. These associations are stored in the HKEY_CLASSES_ROOT
registry. The file extensions are stored in keys with the name of the file
extension. The default value for that key contains a string that identifies
the default application used to open that file type. Under the keys for the
file extensions are the keys for each of the file extension keys' default
application identifier. So, you need to read 2 registry keys for each file
extension. Here is a bit of code that you can use:

using Microsoft.Win32;

public static string GetFileType(string ext)
{
RegistryKey rKey = null;
RegistryKey sKey = null;
string FileType = "";
try
{
rKey = Registry.ClassesRoot;
sKey = rKey.OpenSubKey(ext);
if (sKey != null && (string)sKey.GetValue("", ext) != ext)
{
sKey = rKey.OpenSubKey((string)sKey.GetValue("", ext));
FileType = (string)sKey.GetValue("");
}
else
FileType = ext.Substring(ext.LastIndexOf('.') + 1).ToUpper() + " File";
return FileType;
}
finally
{
if (sKey != null) sKey.Close();
if (rKey != null) rKey.Close();
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 

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,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top