Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Upload File. File Type Question.

Reply
Thread Tools

Upload File. File Type Question.

 
 
Shapper
Guest
Posts: n/a
 
      09-12-2005
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

 
Reply With Quote
 
 
 
 
S. Justin Gengo
Guest
Posts: n/a
 
      09-12-2005
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
"Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message
news:uCeVe$(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
 
Shapper
Guest
Posts: n/a
 
      09-12-2005
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

"S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
message news:Ok#6cL#(E-Mail Removed):

> 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
> "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message
> news:uCeVe$(E-Mail Removed)...
>
> > 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
> >


 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      09-13-2005
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.

"Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message
news:uCeVe$(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File upload from client application (non-form based upload) stuart@microsoft.com Python 1 11-25-2006 12:14 AM
Upload a file without file Upload control - ASP.Net =?Utf-8?B?U2FyYXY=?= ASP .Net 3 08-03-2005 01:09 AM
How to detect sizeof upload in order to show status of browser file upload? Heather Fraser Java 0 07-05-2004 12:35 AM
Upload Excel with Macros using HTML File Upload Prakash ASP General 3 11-12-2003 04:26 AM
JSP upload - detecting file type by mime type? what is application/octet-stream? Stephen Riek Java 0 09-19-2003 03:52 AM



Advertisments