[Zope] Generating text/csv from DTML method, Mime Type?

Eric Walstad Eric@Walstads.net
Fri, 10 Nov 2000 09:57:15 -0800


Here's some DTML I use to generate a CSV file on the fly using data returned
from a query of a MySQL database:

<dtml-call "RESPONSE.setHeader('Content-Type',
'application/x-csv')"><dtml-in qryAllDB>
<dtml-if sequence-start>
FirstName,MiddleName,LastName,Address,City,State,Zip,Phone,Fax,Email,URL,Bir
thday
</dtml-if sequence-start>
<dtml-var FirstName null=''>,<dtml-var MiddleName null=''>,<dtml-var
LastName null=''>,<dtml-var Address null=''>,<dtml-var City
null=''>,<dtml-var State null=''>,<dtml-var Zip null=''>,<dtml-var Phone
null=''>,<dtml-var Fax null=''>,<dtml-var email null=''>,<dtml-var URL
null=''>,<dtml-var Birthday null=''>
</dtml-in>

Notice that formatting is kinda ugly.  That's so that I don't get any
unwanted carriage returns nor any line feeds in the downloaded file.
Also, notice that the first line of the output (after the Content-Type, that
is) is a header line, which you may not want for your output.  I include it
so that when the file is viewed in a spreadsheet, the user can tell what
each column is.

FYI, I found a lot of info on this technique on NIP Ltd's mailing list
archive (Thanks again NIP!!):
http://zope.nipltd.com/public/lists.html


HTH,

Eric.

// -----Original Message-----
// From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of
// Oliver Bleutgen
// Sent: Friday, November 10, 2000 8:12 AM
// To: zope@zope.org
// Subject: Re: [Zope] Generating text/csv from DTML method, Mime Type?
//
//
// > I'm create a CSV file that I would like browsers to get the "save as"
// > dialog when they select it.
//
// > I'm using RESPONSE.write() to write out the CSV file, and I set the
// > Content-Type before calling RESPONSE.write
//
//
// > Netscape seems to work ok, it pops up a save box for "text/csv" and
// > "application/vnd.ms-excel".
//
// > However in both cases IE5.5 just ignores the data and re-requests the
// > page.
//
// > Does anyone know *all* the steps I need to take to get this to work
// > correctly with IE? I must be doing something wrong. I want to supply a
// > CSV file, but generated dynamically. I'm sure I'm missing a header
// > setting or something.
// Adding "Content-Disposition: attachment [; filename=bla.csv]" as
// a http-header might help...
// The part in []s is optional .
// See my mail from yesterday for a bit more info
//
// cheers,
// oliver
//
// _______________________________________________
// Zope maillist  -  Zope@zope.org
// http://lists.zope.org/mailman/listinfo/zope
// **   No cross posts or HTML encoding!  **
// (Related lists -
//  http://lists.zope.org/mailman/listinfo/zope-announce
//  http://lists.zope.org/mailman/listinfo/zope-dev )
//