<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<font size="-1">Hi All,<br>
<br>
As this is my first post to the list I'll do a quick bio. I've
been using Perl since v3 - that was for my ISP business where I
wrote our in-house accounting system for dialup clients in Perl
back in the late 90's. Since then I'va also used it to do the
heavy lifting on a hit counter, sorting 6-8mil hits per day into a
cluster of MySQL servers and various other smaller jobs.<br>
<br>
Fast forward to today and I'm working on a project that is
Catalyst/Postgresql/Starman based and I've struck a problem that
is quite irritating as it's a simple task that looks right but
doesn't act as it should. I'm creating a file and then I want it
to pop up a dialog to download that file. The request is posted
via jquery/ajax, the file is generated and it then sends it back
to the browser with some appropriate headers. Here's the code:<br>
<br>
Javascript/JQuery<br>
===========<br>
<script><br>
$(function() {<br>
<br>
$('#my_button').click(function() {<br>
$.post( "/myexport/id/[% object.myexport_id %]/</font><font
size="-1"><font size="-1">myexport</font>_export", { id: [%
object.</font><font size="-1"><font size="-1">myexport</font>_id
%] }, function( data ) {<br>
});<br>
});<br>
<br>
});<br>
</script><br>
<br>
Pretty straight forward - it posts to the function that is going
to create the export file.<br>
<br>
The Catalyst subroutine<br>
===============<br>
<br>
sub </font><font size="-1"><font size="-1">myexport</font>_export
:Chained('object') PathPart('</font><font size="-1"><font
size="-1">myexport</font>_export') Args(0) {<br>
my ( $self, $c ) = @_;<br>
<br>
$self->check_some stuff($c);<br>
my $result = try {<br>
$self->_do_</font><font size="-1"><font size="-1">myexport</font>($c);<br>
}<br>
catch {<br>
$c->log->error("things_export: $_");<br>
$c->response->code('500'); <br>
$c->response->body("$_");<br>
};<br>
<br>
my ($myexport_fh, $</font><font size="-1"><font size="-1">myexport</font>_fname)
= tempfile();<br>
$</font><font size="-1"><font size="-1">myexport</font>_fh->print($result);<br>
close($</font><font size="-1"><font size="-1">myexport</font>_fh);<br>
<br>
if (-e $</font><font size="-1"><font size="-1">myexport</font>_fname
and -s $</font><font size="-1"><font size="-1">myexport</font>_fname)
{<br>
open $</font><font size="-1"><font size="-1">myexport</font>_fh,
"<". $</font><font size="-1"><font size="-1">myexport</font>_fname;<br>
$c->response->header('Content-Type' =>
'text/csv');<br>
$c->response->header('Content-Disposition' =>
qq[attachment;filename="export.csv"]); <br>
$c->response->body($</font><font size="-1"><font
size="-1">myexport</font>_fh);<br>
unlink($</font><font size="-1"><font size="-1">myexport</font>_fname);<br>
}<br>
else {<br>
# TODO: Error display here!<br>
} <br>
}<br>
<br>
There's a call to _do_myexport in there that extracts and returns
some data, and then we create the tempfile and (hopefully) send it
back to the browser.<br>
<br>
The Result<br>
=======<br>
<br>
Chrome is telling me that the Content-Type, Length and Disposition
headers are being sent back, but the browser isn't popping up a
download dialog:<br>
<br>
</font><br>
<meta charset="utf-8">
<ol class="outline-disclosure" tabindex="0" style="box-sizing:
border-box; list-style-type: none; -webkit-padding-start: 12px;
margin: 0px; outline: none; color: rgb(48, 57, 66); font-family:
'Lucida Grande', sans-serif; font-size: 12px; font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal;
line-height: normal; orphans: auto; text-align: start;
text-indent: 0px; text-transform: none; white-space: normal;
widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;
background-color: rgb(255, 255, 255);">
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Request URL:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top: 1px;"><a class="moz-txt-link-freetext" href="http://10.211.55.5:5000/">http://10.211.55.5:5000/</a><font
size="-1">myexport</font>/id/210/<font size="-1">myexport</font>_export</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Request Method:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top: 1px;">POST</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Status Code:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top: 1px;">200
OK</div>
</li>
<li title="" class="parent expanded" style="box-sizing:
border-box; padding: 0px 0px 0px 14px; margin-top: 1px;
margin-left: -12px; word-wrap: break-word; -webkit-user-select:
none; font-weight: bold;">Request Headers<span
class="header-toggle" style="box-sizing: border-box; display:
inline; margin-left: 30px; font-weight: normal; color:
rgb(115, 115, 115);">view source</span></li>
<ol class="children expanded" style="box-sizing: border-box;
position: relative; margin: 0px; cursor: default; min-width:
100%; padding: 2px 6px !important; list-style-type: none;
-webkit-padding-start: 12px; display: block;">
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Accept:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">*/*</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Accept-Encoding:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">gzip,deflate,sdch</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Accept-Language:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">en-US,en;q=0.8</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Connection:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">keep-alive</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Content-Length:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">6</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Content-Type:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">application/x-www-form-urlencoded; charset=UTF-8</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">Cookie:<font size="-1">myexport</font>_session=9b8eca81032e0dc3c906d701974ff7e42d841a9f</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Host:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">10.211.55.5:5000</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Origin:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;"><a class="moz-txt-link-freetext" href="http://10.211.55.5:5000">http://10.211.55.5:5000</a></div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Referer:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;"><a class="moz-txt-link-freetext" href="http://10.211.55.5:5000/">http://10.211.55.5:5000/</a><font size="-1">myexport</font>/id/210/my_button_page</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">User-Agent:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77
Safari/537.36</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">X-Requested-With:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">XMLHttpRequest</div>
</li>
</ol>
<li title="" class="parent expanded" style="box-sizing:
border-box; padding: 0px 0px 0px 14px; margin-top: 1px;
margin-left: -12px; word-wrap: break-word; -webkit-user-select:
none; font-weight: bold;">Form Data<span class="header-toggle"
style="box-sizing: border-box; display: inline; margin-left:
30px; font-weight: normal; color: rgb(115, 115, 115);">view
source</span><span class="header-toggle" style="box-sizing:
border-box; display: inline; margin-left: 30px; font-weight:
normal; color: rgb(115, 115, 115);">view URL encoded</span></li>
<ol class="children expanded" style="box-sizing: border-box;
position: relative; margin: 0px; cursor: default; min-width:
100%; padding: 2px 6px !important; list-style-type: none;
-webkit-padding-start: 12px; display: block;">
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">id:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">210</div>
</li>
</ol>
<li title="" class="parent expanded" style="box-sizing:
border-box; padding: 0px 0px 0px 14px; margin-top: 1px;
margin-left: -12px; word-wrap: break-word; -webkit-user-select:
none; font-weight: bold;">Response Headers<span
class="header-toggle" style="box-sizing: border-box; display:
inline; margin-left: 30px; font-weight: normal; color:
rgb(115, 115, 115);">view source</span></li>
<ol class="children expanded" style="box-sizing: border-box;
position: relative; margin: 0px; cursor: default; min-width:
100%; padding: 2px 6px !important; list-style-type: none;
-webkit-padding-start: 12px; display: block;">
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Connection:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">keep-alive</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Content-Disposition:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">attachment;filename="export.csv"</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Content-Length:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">124</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Content-Type:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">text/csv</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Date:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">Tue, 21 Jan 2014 00:01:17 GMT</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">Set-Cookie:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">myexport_session=9b8eca81032e0dc3c906d701974ff7e42d841a9f;
path=/; expires=Tue, 21-Jan-2014 02:01:17 GMT; HttpOnly</div>
</li>
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap:
break-word; white-space: nowrap;">
<div class="header-name" style="box-sizing: border-box; color:
rgb(84, 84, 84); display: inline-block; margin-right: 0.5em;
font-weight: bold; vertical-align: top; white-space:
pre-wrap;">X-Catalyst:</div>
<div class="header-value source-code" style="box-sizing:
border-box; font-family: Menlo, monospace; white-space:
pre-wrap; font-size: 11px !important; display: inline;
margin-right: 100px; word-break: break-all; margin-top:
1px;">5.90053</div>
</li>
</ol>
</ol>
<p><br>
I have a nasty feeling that I'm missing something obvious here but
I've gone over this so many times that I think/hope a fresh set of
eyes will help find the problem. The data does get sent back to
the browser but as data rather than a file to download.<br>
</p>
<p>Regards,<br>
</p>
<p>Gary Smith<br>
<br>
</p>
<ol class="outline-disclosure" tabindex="0" style="box-sizing:
border-box; list-style-type: none; -webkit-padding-start: 12px;
margin: 0px; outline: none; color: rgb(48, 57, 66); font-family:
'Lucida Grande', sans-serif; font-size: 12px; font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal;
line-height: normal; orphans: auto; text-align: start;
text-indent: 0px; text-transform: none; white-space: normal;
widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;
background-color: rgb(255, 255, 255);">
<li title="" style="box-sizing: border-box; padding: 0px 0px 0px
14px; margin-top: 1px; margin-left: -2px; word-wrap: break-word;
white-space: nowrap;"><br>
</li>
</ol>
</body>
</html>