Page 1 of 1

Changing the MIME type so a page returns a CSV file?

Posted: Tue Feb 07, 2006 2:34 am
by ChrisC
Hi,

I have a page that I want to be served with a MIME type "text/csv" so that it opens with Excel or a similar app that is registered to handle comma-separatated value files.  Is there any way I can change the mime type (and, ideally, extension) for a CMSMS page?

I've tried something like "," but that doesn't seem to override.  I might be able to figure out a mod_rewite in Apache, but doing it from CMSMS would be more clean and portable.

Thanks,


CJC

Re: Changing the MIME type so a page returns a CSV file?

Posted: Tue Feb 07, 2006 9:19 am
by jelle
Just discovered that you can define your own tags (under extensions). The tag itself is made up of a snippet of normal php code. With php you could change any header any way you like, so probably you could change it to a .csv  file. 
If the header is changed later in the processing you might be out of luck though, but I think it is worth the experiment.

Re: Changing the MIME type so a page returns a CSV file?

Posted: Tue Feb 07, 2006 1:42 pm
by jelle
ok, instead of just thinking out loud it tried this out: (it didn't work...)
first I defined a user defined tag like this:
name: csv-mimetype

Code: Select all

 header("Content-Type: text/csv; Extension=.cvs"); 
then defined a template csv-mimetype with this content:

Code: Select all

{csv-mimetype}
{content}
and finally wrote a page with one line containing 1,2,3,4,5,6,7 etc.

Clicking on the link in the menu(called csv_test.html just opened the file in my browser. cmsms had also appended these two lines:

Code: Select all

<!-- Generated in 0.319889 seconds by CMS Made Simple 0.11.2 (cached) using 9 SQL queries -->
<!-- CMS Made Simple - Released under the GPL - http://cmsmadesimple.org -->
Either I am doing something wrong (eg. didn't use the tags feature correctly) or this is not the way to do it.

Re: Changing the MIME type so a page returns a CSV file?

Posted: Wed Feb 08, 2006 1:23 pm
by ChrisC
Thanks for the suggestions.  They were certainly on the right track.  A custom tag worked, but my solution's a bit hacky.  Turns out that a Content-Type header is sent by index.php, and that trumps any header sent from a user-defined tag.  However, since the header comes from the global variables, it's possible to override the value.

Code: Select all

global $gCms;
$gCms->variables['content-type'] = "application/vnd.ms-excel";
header("Content-disposition: attachment; filename=report.csv");
Turns out the text/csv MIME type isn't registered by default with browsers (although there's an RFC for it), but an Excel-specific MIME type works on all my Windows boxes.  I added a Content-disposition header so that the filename doesn't show up as index.php.

I don't know for certain if there are any side-effects to changing that global.  If an 'if' statement looks at the value after I set it, results could be unpredictable.  Plus, the code depends on external behavior that could change in a future CMSMS release.  But, hey, it works.  Further testing is in order...