How to set mime types

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • colinod
    Contributor
    • Nov 2007
    • 347

    How to set mime types

    I have a web page with various mp3 files on it adn am trying to pigure out how to set a mime type for downloads because i have some people who are downloading the files and they are being given a .mpeg file extension.

    The page is written in asp and have tried

    Code:
    <% Response.ContentType = "audio/mpeg" %>
    but this just makes the page open and try and load quicktime.

    Not even sure if this would be the right area of the website

    thanks

    colin
  • Nicodemas
    Recognized Expert New Member
    • Nov 2007
    • 164

    #2
    The way I have always done the delivery of file types of this nature is to use an ADO Stream.

    Here's the idea:

    1. Determine which file the user wants to download.
    2. Ensure the file exists.
    3. Get the absolute filepath of the file.

    4. Add a Content-Disposition header to the response :
    Code:
    Response.AddHeader "Content-Disposition", "attachment; filename=" & YOUR-FILE-NAME

    5. Add the file's size as a Content-Length header to the response :
    Code:
    Response.AddHeader "Content-Length", YOUR-FILES-FILESIZE
    6. Now open an ADO Stream in binary mode:

    Code:
    Set objStream = Server.CreateObject("ADODB.Stream")  
    objStream.Open  
    objStream.Type = 1  
    Response.CharSet = "UTF-8"
    7. Send the binary stream to the user's browser:

    Code:
    Response.BinaryWrite(objStream.Read)
    8. Close down used objects

    Code:
    objStream.Close  
    Set objStream = Nothing

    NOTE: It is important to note that you shouldn't include anything else in the output when the file is found and set as binary data to the user; as this would result in a corrupt download.
    Last edited by Nicodemas; Sep 29 '10, 11:06 PM. Reason: Added "NOTE:"

    Comment

    Working...