Upload File. File Type Question.

Discussion in 'ASP .Net' started by Shapper, Sep 12, 2005.

  1. Shapper

    Shapper Guest

    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
    Shapper, Sep 12, 2005
    #1
    1. Advertising

  2. 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$...
    > 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. Justin Gengo, Sep 12, 2005
    #2
    1. Advertising

  3. Shapper

    Shapper Guest

    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#:

    > 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$...
    >
    > > 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
    > >
    Shapper, Sep 12, 2005
    #3
  4. 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$...
    > 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
    >
    Kevin Spencer, Sep 13, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FyYXY=?=

    Upload a file without file Upload control - ASP.Net

    =?Utf-8?B?U2FyYXY=?=, Aug 2, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,434
    Bruce Barker
    Aug 3, 2005
  2. Stephen Riek
    Replies:
    0
    Views:
    4,432
    Stephen Riek
    Sep 19, 2003
  3. Heather Fraser
    Replies:
    0
    Views:
    621
    Heather Fraser
    Jul 5, 2004
  4. Replies:
    1
    Views:
    924
    Gabriel Genellina
    Nov 25, 2006
  5. Prakash
    Replies:
    3
    Views:
    452
    Ray at
    Nov 12, 2003
Loading...

Share This Page