Click to See Complete Forum and Search --> : Force download of Excel csv file


BananaQuaalude
12-16-2003, 10:15 PM
Hello,

I've got a csv file that I build in ASP and then want the user to download. Currently I build the file on the server, then add a link to it on the page.

It works fine, except it always opens in Excel inside the browser (this is for an Intranet, we use MS Office and IE 6.0 standard). I want to force users to download the file.

I've tried changing the file extension, but IE will just recognize it as a text file and open it in the browser displayed as text.

How can I force a download?

Thanks!

jdavia
12-16-2003, 10:56 PM
One posibility is to Zip it in WinZip32, then make a self opening exe file of it, using the Wzsepe32.exe file in WinZip32.
As you know an exe file will download rather than open on the internet
Many persons won't open an exe file for fear of viruses.

BananaQuaalude
12-16-2003, 11:17 PM
Well I'm on an Intranet, so no one should be worried about viruses, at least hopefully not from anything I'm providing them with.

But I'm not opposed to zipping it. That would decrease file size and hence the amount of network traffic.

How do I zip a file in ASP code?

jdavia
12-17-2003, 12:17 AM
You don't. You must use WinZip to zip it.
When it is opened in Excel or wherever, save the file on the computer and zip it as mentioned. Then place it on your server.
A zipped file will download but will have to be unzipped. Change it to a zipped exe it downloads and opens with a click.

BananaQuaalude
12-17-2003, 11:57 AM
Yeah- that's not going to work then.

I'm dynamically generating these csv files in ASP code and there is no manual touch to it. Everything needs to be done in code.

So is there any other way to force a download?

ray326
12-17-2003, 02:38 PM
There is no way to guarantee IE will present a Save as... dialog. On a real browser you could send an application/octet-stream content type and maybe an attachment file-disposition but IE will "sniff" the input stream and make its own decisions regarding what it "thinks" the response is.

BananaQuaalude
12-17-2003, 06:55 PM
Yeah, that's what I was beginning to think.

As if I needed another reason to be frustrated with Microsoft.

Oh well- if I find a way to trick it into thinking it's something it can't identify and force the download I'll post it.

Thanks to both of you for the ideas and information!

jacknbey
04-10-2005, 10:15 PM
came across this thread and I still have the same issue with my website. Has anyone derive any solution to this?

phpnovice
04-10-2005, 11:27 PM
There is no way to guarantee IE will present a Save as... dialog. On a real browser ...
I really hate to be critical, but I sure wish people wouldn't shoot their mouths off when they don't know what they are talking about. Then, in the process, make extravagant claims about how much better other browsers are than IE. I'm no big fan of IE nor MS, but MS products are not the garbage some people try to make them out to be. Also, such "wonderfully intelligent persons" grossly overplay the supposed importance of the so-called almighty "application/octet-stream" MIME-type. HA!!!

I've used the following ASP code to "force" download all kinds of text and binary files -- with their correct MIME-type -- with absolutely no problems whatsoever. Using this code, IE always opens the Save As dialog box -- no matter what MIME-type I specify -- including "text/plain". In my experience, it is the other browsers (not IE) that sometimes ignore the "attachment" property specified in the "content-disposition" HTTP header and, thusly, go ahead and display text file contents in the browser instead of downloading them.

So, here is the best and most correct answer for downloading any desired file type and content -- without hassles. Naturally, this code may require adjustments to suit your exact needs.
'
' ----- Download handler -----
'
Set adoStream = Server.CreateObject("ADODB.Stream")
adoStream.Open()
adoStream.Type = adTypeBinary
adoStream.LoadFromFile Server.MapPath(filePath & fileName)
If adoStream.Type = adTypeBinary Then
stream = adoStream.Read()
Else
stream = adoStream.ReadText()
End If
adoStream.Close()
Set adoStream = Nothing
'
Response.Buffer = True
Response.AddHeader "content-disposition", "attachment; filename=" & fileName
Response.AddHeader "content-length", Len(stream)
Response.ContentType = mimeType
Response.BinaryWrite stream
Response.Flush

mags755
04-12-2005, 03:02 PM
<%
Option Explicit
%>
<!-- METADATA TYPE="typelib"
FILE="C:\Program Files\Common Files\System\ado\msado15.dll" -->
<%
dim fn
fn = "QUERYRESULTS.xls"
Response.Buffer = TRUE
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition","attachment;filename=" & fn




Dim strEmployee, strConnect, objCommand, objRS, objConn



strConnect = "Provider=SQLOLEDB; Persist Security Info=False;" & _
"User ID=sa; Initial Catalog=DBS_opportunity;" & _
"Initial File Name=C:\MSSQL7\Data\DBS_opportunitySQL.mdf"

Set objCommand = Server.CreateObject("ADODB.Command")
objCommand.ActiveConnection = strConnect
objCommand.CommandText = "SELECT * FROM tblOpportunities ORDER BY EmployeeName"
objCommand.CommandType = adCmdText


set objRS = objCommand.Execute
set objCommand = Nothing

dim fld


%>
<HTML>
<BODY>
<TABLE border="1" cellpadding="0" cellspacing="0" bordercolor="#a9a9a9">
<TR>
<%
With objRS
For Each fld In .Fields
With fld
%>
<TH bgcolor="#003468">
<Font face="Tahoma" size="3" color="White">
<%
=.Name

%></FONT>
</TH>
<%
End With
Next 'fld
End With
%>
</TR>
<%

'--Reset Pointer To First Row...
objRS.MoveFirst()

Do Until objRS.EOF
%>
<TR>
<%
With objRS
For Each fld in .Fields
With fld
%>
<TD><%=.Value%></TD>
<%
End With
Next 'fld
%>
</TR>
<%
.MoveNext()
End With
Loop
%>
</TABLE>
<%
objRS.Close
Set objRS = Nothing
%>
</BODY>
</HTML>

IMALAN
04-26-2005, 04:13 AM
Thanks, phpnovice. Ur codes worked. :)

phpnovice
04-26-2005, 08:55 AM
Cheers.