Dojo 1.9 AJAX Post and Excel::Writer::XLSX

515 views
Skip to first unread message

Ian

unread,
Jan 29, 2014, 9:58:29 AM1/29/14
to spreadsheet...@googlegroups.com
Hello Mr. Mcnam,

I am wondering if anyone has tested sending a .xlsx attachment to a browser using Dojo 1.9. Using the cgi.pl example and plain JavaScript AJAX Post gets the end result I am looking for. When attempting the same with Dojo no such luck. Could be my limited understanding of dojo, but Firebug console shows a response, in strange characters indicative of binary as I have recently learned. However it is not entering the if(resonse) statement. Am I missing something unique to Dojo with regards to the proper Content type?

I have tried several methods including adding and removing the bold line. Here is the html code:

<!DOCTYPE html>
<html>
<head>
    <script src='../../lib/js_scripts/dojo-release-1.9.1/dojo/dojo.js'></script>
    <script>
        function callExcel()
        {
            require(["dojo/request","dojo/json"],function(request,json)
            {
                request.post("url", {
                    handleAs: "json",
                    //headers: {"Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}
                }).then(function(response){
                    if(response)
                    {
                        alert("success");
                        //window.open("url","_blank");                //url (both in post and window.open) does exist , just cant put it out here.
                    }
                });
            });
        }
    </script>
</head>
<body>
    <div id="MyDiv">This is not going well</div>
    <button type="button" onClick="callExcel();">click_me</button>
</body>
</html>


Here is the cgi:

use strict;
use warnings;
use Excel::Writer::XLSX;
use utf8;

my $cgi = CGI->new();
1;
my $workbook  = '';
my $worksheet  = '';


my $filename  = "MasterList.xlsx"; 

print "Content-Disposition: attachment; filename=$filename\n";
print "Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\n\n";
#print $cgi->header(-type=>"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", -attachment=>$filename);

binmode(STDOUT);

$workbook = Excel::Writer::XLSX->new(\*STDOUT);
$worksheet = $workbook->add_worksheet();

# Set the column width for column 1
$worksheet->set_column(0, 0, 20);

# Create a format
my $format = $workbook->add_format();
$format->set_bold();
$format->set_size(15);
$format->set_color('blue');

# Write to the workbook
$worksheet->write(0, 0, "Hi Excel!", $format);
$workbook->close();

exit(0);

jmcnamara

unread,
Feb 3, 2014, 6:10:56 AM2/3/14
to spreadsheet...@googlegroups.com


On Wednesday, 29 January 2014 14:58:29 UTC, Ian wrote:

I am wondering if anyone has tested sending a .xlsx attachment to a browser using Dojo 1.9.

Hi Ian,

I have very little experience with Dojo.

Anyone else have any ideas based on Dojo or similar JS frameworks?

John

GainfulShrimp

unread,
Feb 3, 2014, 7:14:27 AM2/3/14
to spreadsheet...@googlegroups.com
I haven't used Dojo, but I've hit very similar issues with jQuery in the past.

Essentially, the problem boils down to the fact that the Ajax stuff is built to handle asynchronous loading of web data, not binary downloads.

In your example, for instance, the code seems to tell Dojo to expect 'json' back from the server... but it's obviously not getting json - it's getting binary.  That maybe why the results are wrong?

I would recommend the following approach to work-around this issue:

1. When the Ajax call requests the file, have the server create a temporary Excel file on disk with a unique name, responding to the client (with Json) that creation is successful and indicating a special URL where some other serverside code emits the file and then deletes the temporary file afterwards.
2. If your Dojo code sees success in the (json format) response to step 1, it creates a hidden iframe object and specifies the source for the iframe as the special URL also contained in the response from step 1 
3.The browser should then present the user with a download prompt (if not, change the 'special URL' response content type to be octet-stream etc).

It's a bit faffy, but I used it reliably myself a while back.  I hope that helps!  :)

Cheers,

Matt

Ian

unread,
Feb 3, 2014, 1:52:29 PM2/3/14
to spreadsheet...@googlegroups.com
Matt,

Thanks for pointing out the handler issue. I switched the handleAs to XML, and bam worked right away. I asked a similar question a few days ago on stack overflow, http://stackoverflow.com/questions/21342323/excelwriterxlsx-not-sending-an-attachment. If you are a member and would like to contribute, please do so, if not I will provide the answer and give you credit.

Thanks
Ian

GainfulShrimp

unread,
Feb 4, 2014, 2:46:39 AM2/4/14
to spreadsheet...@googlegroups.com
Hi Ian,

I'm glad you've got it working; I know from painful experience that this stuff can get frustrating, even with Firebug etc!  

IMHO, it's worth testing in a few different browsers though (if you need it to work widely), as it seems a bit inconsistent to me that the XML option works for a binary response when the Json option doesn't... you're not returning either Json or XML, after all.  It might just be that the exception handling code for XML is more robust than that for Json, I guess.

I have commented on your SO post as you suggested, thanks!

Cheers,

Matt

Ian

unread,
Feb 4, 2014, 9:35:10 AM2/4/14
to spreadsheet...@googlegroups.com
Matt,

I had checked it right away on both firefox (several versions..unfortunately) and IE8. No chrome or safari here. It worked out quite nicely.

Thanks Again
Ian
Reply all
Reply to author
Forward
0 new messages