How to specify Mime filename in ASP page?

Discussion in 'ASP General' started by SW, Aug 27, 2003.

  1. SW

    SW Guest

    I have an ASP page that causes Excel to be loaded on the client PC
    with the text sent from within the page. An excerpt of it is shown
    below:

    With Response
    .ContentType = "application/vnd.ms-excel"
    .Write(strResponse)
    .Flush
    .End
    End With

    where strResponse is the text to be displayed in Excel.

    This all works fine and as expected, but the only problem is that the
    file is opened in Excel on the client with a filename that reflects
    the page from which the information originated, e.g:

    http://myserver/mypage.asp

    Is there any way of specifying the filename using MIME, for example if
    I wanted the file to be loaded in Excel as "myfile.xls"?

    Many thanks in advance
     
    SW, Aug 27, 2003
    #1
    1. Advertising

  2. You need to add the Content-Disposition header:

    Response.AddHeader "content-disposition", "inline; filename=""" &
    server.URLPathEncode("MyFileName.csv") & """"

    There is a small bug in IE's handling of content-disposition. The choices
    are "inline" or "attachment"... but if the word attachment appears anywhere
    in the header's value then the response will be treated as an attachment. So
    the following would force the user to save the file:
    Response.AddHeader "content-disposition", "inline; filename=""" &
    server.URLPathEncode("Attachment.csv") & """"

    I work around this by replacing the word Attachment with Attachmnt... not
    great... but it does solve the problem.

    Also be careful about extra periods in the filename. It seems a filename
    like "a.b.txt" comes out at "a.b.txt[1]", but if you escape the first period
    then things work: "a%2eb.txt" comes out as "a.b.txt". I use the following:
    strfilename = replace(left(strfilename, instr(1, strfilename, "." &
    fso.GetExtensionName(strFilename)) - 1), ".", "%2e") & "." &
    fso.GetExtensionName(strFilename)

    Not very elegant... but it does work.

    Regards,

    Rob
    --
    RMTrack a new bug tracking tool. Available now from www.rmtrack.com (watch
    for our new 1.2 release!)


    "SW" <> wrote in message
    news:...
    > I have an ASP page that causes Excel to be loaded on the client PC
    > with the text sent from within the page. An excerpt of it is shown
    > below:
    >
    > With Response
    > .ContentType = "application/vnd.ms-excel"
    > .Write(strResponse)
    > .Flush
    > .End
    > End With
    >
    > where strResponse is the text to be displayed in Excel.
    >
    > This all works fine and as expected, but the only problem is that the
    > file is opened in Excel on the client with a filename that reflects
    > the page from which the information originated, e.g:
    >
    > http://myserver/mypage.asp
    >
    > Is there any way of specifying the filename using MIME, for example if
    > I wanted the file to be loaded in Excel as "myfile.xls"?
    >
    > Many thanks in advance
     
    Rob MacFadyen, Aug 27, 2003
    #2
    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?YWQ=?=

    How to specify filename when exporting to EXCEL

    =?Utf-8?B?YWQ=?=, Sep 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    514
    Steve C. Orr [MVP, MCSD]
    Sep 28, 2004
  2. CJ
    Replies:
    1
    Views:
    1,602
    Andrew Thompson
    Oct 29, 2004
  3. Jan Arickx
    Replies:
    0
    Views:
    216
    Jan Arickx
    Aug 25, 2003
  4. joe
    Replies:
    0
    Views:
    212
  5. ecureuil
    Replies:
    0
    Views:
    356
    ecureuil
    May 28, 2006
Loading...

Share This Page