Files Types

Discussion in 'ASP .Net' started by shapper, Oct 19, 2007.

  1. shapper

    shapper Guest

    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
    shapper, Oct 19, 2007
    #1
    1. Advertising

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

    "shapper" <> wrote in message
    news:...
    > 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, Oct 19, 2007
    #2
    1. Advertising

  3. "shapper" <> wrote in message
    news:...

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


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Oct 20, 2007
    #3
    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. Sathyaish
    Replies:
    2
    Views:
    587
    Sathyaish
    May 22, 2005
  2. Soren Kuula
    Replies:
    2
    Views:
    561
    Henry S. Thompson
    Dec 1, 2005
  3. Jeremy Cowles

    STD types vs C++ intrinsic types

    Jeremy Cowles, Aug 18, 2003, in forum: C++
    Replies:
    5
    Views:
    1,884
    Bob Jacobs
    Aug 19, 2003
  4. Steve Knight
    Replies:
    2
    Views:
    754
    Steve Knight
    Oct 10, 2003
  5. Manlio Perillo

    missing types in module types?

    Manlio Perillo, May 29, 2004, in forum: Python
    Replies:
    0
    Views:
    351
    Manlio Perillo
    May 29, 2004
Loading...

Share This Page