How to specify Mime filename in ASP page?

S

SW

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
 
R

Rob MacFadyen

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top