dynamically return another file type

Discussion in 'ASP .Net' started by Tim Mulholland, Dec 15, 2003.

  1. I am trying to create an ASPX page that, based on QueryString inputs,
    returns another file, in this case a WMA file. I do not want to do this by
    redirect, because these files are not available publicly via the web, or via
    a share. So i am returning it by opening a stream, and writing the bytes out
    via Response.BinaryWrite().
    This is all fine and dandy, but the Save As.. dialog lists the filetype as
    ASPX page, and gives the page name...
    how do i get it to list something else, and with the filetype being WMA (or
    whatever i want)?
    Is this even possible?

    TIA,

    Tim
     
    Tim Mulholland, Dec 15, 2003
    #1
    1. Advertising

  2. Tim Mulholland

    Shawn Guest

    Here is an example of how to download or open a word document in you browser
    using a FileStream.
    In your case try using "video/x-ms-wma" as the ContentType.

    Dim fs As New FileStream("c:\docs\0000001F.DOC", FileMode.OpenOrCreate,
    FileAccess.Read)
    Dim MyData(fs.Length) As Byte
    fs.Read(MyData, 0, fs.Length)
    Response.Buffer = True
    Response.Clear()
    Response.ContentType = "application/msword"

    'Opens outside of the browser
    'Response.AddHeader("content-disposition", "attachment;
    filename=0000001F.DOC")
    'Opens inside of the browser
    Response.AddHeader("content-disposition", "inline; filename=0000001F.DOC")

    Response.BinaryWrite(MyData)
    Response.End()

    Shawn


    "Tim Mulholland" <> wrote in message
    news:...
    I am trying to create an ASPX page that, based on QueryString inputs,
    returns another file, in this case a WMA file. I do not want to do this by
    redirect, because these files are not available publicly via the web, or via
    a share. So i am returning it by opening a stream, and writing the bytes out
    via Response.BinaryWrite().
    This is all fine and dandy, but the Save As.. dialog lists the filetype as
    ASPX page, and gives the page name...
    how do i get it to list something else, and with the filetype being WMA (or
    whatever i want)?
    Is this even possible?

    TIA,

    Tim
     
    Shawn, Dec 15, 2003
    #2
    1. Advertising

  3. Attempted to try your example code this morning, but it seems i'm having
    troubles getting into my development server... this code makes sense though,
    so thanks for your help.
    If i have any more questions once i get into my server, i'll post again, but
    i'd guess this works.
    Thanks again,

    Tim

    "Shawn" <> wrote in message
    news:3fdd66c0$...
    > Here is an example of how to download or open a word document in you

    browser
    > using a FileStream.
    > In your case try using "video/x-ms-wma" as the ContentType.
    >
    > Dim fs As New FileStream("c:\docs\0000001F.DOC", FileMode.OpenOrCreate,
    > FileAccess.Read)
    > Dim MyData(fs.Length) As Byte
    > fs.Read(MyData, 0, fs.Length)
    > Response.Buffer = True
    > Response.Clear()
    > Response.ContentType = "application/msword"
    >
    > 'Opens outside of the browser
    > 'Response.AddHeader("content-disposition", "attachment;
    > filename=0000001F.DOC")
    > 'Opens inside of the browser
    > Response.AddHeader("content-disposition", "inline; filename=0000001F.DOC")
    >
    > Response.BinaryWrite(MyData)
    > Response.End()
    >
    > Shawn
    >
    >
    > "Tim Mulholland" <> wrote in message
    > news:...
    > I am trying to create an ASPX page that, based on QueryString inputs,
    > returns another file, in this case a WMA file. I do not want to do this by
    > redirect, because these files are not available publicly via the web, or

    via
    > a share. So i am returning it by opening a stream, and writing the bytes

    out
    > via Response.BinaryWrite().
    > This is all fine and dandy, but the Save As.. dialog lists the filetype as
    > ASPX page, and gives the page name...
    > how do i get it to list something else, and with the filetype being WMA

    (or
    > whatever i want)?
    > Is this even possible?
    >
    > TIA,
    >
    > Tim
    >
    >
    >
     
    Tim Mulholland, Dec 15, 2003
    #3
  4. Hi Tim,

    Thanks for posting in the group.

    I agree with Shawn. ContentType should do the trick for you. It could tell
    client browser how to deal with the data stream in the http response.

    If you have any more concerns on it, please feel free to post in the group.

    Best regards,
    Yanhong Huang
    Microsoft Community Support

    Get Secure! ¨C www.microsoft.com/security
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Yan-Hong Huang[MSFT], Dec 16, 2003
    #4
  5. I finally was able to test my code.
    I was using ContentType, but it was the AppendHeader, content-disposition
    stuff that got me going in the right direction.
    Seems to work beautifully.
    Thanks to all,

    Tim

    "Yan-Hong Huang[MSFT]" <> wrote in message
    news:...
    > Hi Tim,
    >
    > Thanks for posting in the group.
    >
    > I agree with Shawn. ContentType should do the trick for you. It could tell
    > client browser how to deal with the data stream in the http response.
    >
    > If you have any more concerns on it, please feel free to post in the

    group.
    >
    > Best regards,
    > Yanhong Huang
    > Microsoft Community Support
    >
    > Get Secure! ¨C www.microsoft.com/security
    > This posting is provided "AS IS" with no warranties, and confers no

    rights.
    >
     
    Tim Mulholland, Dec 16, 2003
    #5
  6. Hi Tim,

    Ahh, Then I should say thanks to Shawn again. :) I haven't noticed that
    contect-disposition is so important under this situation. Thanks very much
    for your response.

    I just did a quick search in MSDN and found this info:

    -----------
    Content-disposition is an extension to the MIME protocol that instructs a
    MIME user agent on how it should display an attached file. The range of
    valid values for content-disposition are discussed in Request for Comment
    (RFC) 1806 (see the "References" section of this article). This article
    focuses on the "attachment" argument, which instructs a user agent (in this
    case, Internet Explorer) to save a file to disk instead of saving it inline.

    When Internet Explorer receives the header, it raises a File Download
    dialog box whose file name box is automatically populated with the file
    name that is specified in the header. (Note that this is by design; there
    is no way to use this feature to save a document to the user's computer
    without prompting him or her for a save location.)

    There are two ways that you can use Internet Explorer to specify a
    content-disposition header for a file: dynamically and statically.

    To apply the header dynamically, create an Active Server Pages (ASP) file
    that writes the document out to the browser. Use the Response.AddHeader
    method to add the content-disposition header. For example:
    Response.AddHeader "content-disposition","attachment; filename=fname.ext"

    Instructions on how to perform a binary write for nontext documents are
    available in the following Microsoft Knowledge Base article:
    193998 HOWTO: Read and Display Binary Data in ASP

    This technique is ideal when you want to protect a document store on your
    server, especially one that exists outside of the Web root.

    To apply the header statically, right-click the document in the Internet
    Service Manager, and then select Properties. Select the HTTP Headers tab
    and enter the content-disposition header there. This works best when you
    only want to apply content-disposition to a few files on your system and
    don't require the overhead of ASP.

    Please note that this solution will not work in Internet Explorer 4.01 due
    to a bug. For additional information, click the article number below to
    view the article in the Microsoft Knowledge Base:
    182315 FIX: Content-Disposition: Does Not Force File Download Dialog
    -----------

    RFC 1806, RFC 2183 describe more on it.

    Anyway, I am glad that the problem is resolved. If you have any more
    questions, pleaes feel free to post in the group.

    Thanks again for participating the community.

    Best regards,
    Yanhong Huang
    Microsoft Community Support

    Get Secure! ¨C www.microsoft.com/security
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Yan-Hong Huang[MSFT], Dec 17, 2003
    #6
    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. DaKoadMunky
    Replies:
    8
    Views:
    3,315
    Leor Zolman
    May 14, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    835
    Keith Thompson
    Mar 6, 2005
  3. gk245
    Replies:
    2
    Views:
    1,210
    Christopher Benson-Manica
    May 6, 2006
  4. msimmons
    Replies:
    0
    Views:
    501
    msimmons
    Jul 16, 2009
  5. andrew cooke
    Replies:
    1
    Views:
    182
    Steven D'Aprano
    Apr 3, 2011
Loading...

Share This Page