rashley has asked for the wisdom of the Perl Monks concerning the following question:

I have a CGI that calls a C++ executable (running on AIX) that generates a report in XML that applies a stylesheet already present on the user's local machine.

I then feed the report back to the browser with the following function.

sub retrieveFile { my $file = shift; if(not -e $file) { croak "Could not locate file: $file)"; } if(not open(FH, $file)) { croak "Could not open file: $file"; } my $filesize = -s $file; print $cgi->header(-type=>'application/vnd.ms-excel', -Content-Disposition=>"attachment; filename +=$file", -Content_Length=>$filesize); while(<FH>) { print; } close FH; }

So when this is run, you get the standard "Open or Save" windows dialog.

If you choose "Save" and then open it, you get the dialog to apply the stylesheet. It opens in Excel and all is right with the world.

But if you choose "Open", you get the dialog, then an error message that says: "The download of the specified resource has failed.", then you get the stylesheet dialog AGAIN, then the file opens and looks fine.

Since the file is fine when you save, I don't see how the problem could be in the XML being generated, which means that the problem is in my Perl somewhere.

Any thoughts, please? Thanks.

Replies are listed 'Best First'.
Re: Openning XML in Excel using CGI
by Joost (Canon) on Nov 29, 2006 at 15:55 UTC
      I've tried ms-excel, excel, and text/xml, all of which open the file in a text editor. Still looking for other MIME types that will launch Excel.
Re: Openning XML in Excel using CGI
by BaldPenguin (Friar) on Nov 29, 2006 at 16:38 UTC
    I can only assume that your users are on Windows. If that is the case there are a couple of things that will throw things off.

    First be sure that your XML doesn't have any empty lines at the beginning. Also be sure that your namespaces match the version of Excel opening the files. He-Who-Must-Not-Be-Named has changed the namespaces AND functionality over the last three versions of SpreadSheetML.

    Also check your xslt processor. I use the libxslt libraries through XML::LibXSLT and have to do some doctoring after the transform to get my namespaces correct.

    Also, I think you touched on this at the last of your post, but saving the file to the file system and then opening is always a good step. But remember this, IE has a habit of overruling mime-type with file extension. In some cases past I have had the mime-type overruled in favor of the mime-type assoiciated with the file extension, and if you give it a file extension it doesn't have, it bails even if you gave it a mime type to use. So when you save the file:

    What file extension to you give it?
    Do you open by double-clicking or by the 'Open ...' menu?
    Which version of excel do you or your users use?

    Don
    WHITEPAGES.COM | INC
    Everything I've learned in life can be summed up in a small perl script!
      You are correct, the users on on Windows.

      I'm not explicitly setting a file extension or name. Using vnd.ms-excel, it comes through as the name of my CGI with .xls tacked on the end.

      The 'Open' menu is what's giving me the touble.

      Most of my users are on Excel 2002, SP3. But this may not always be the case.

        Try changing the file extension or explicitly setting it to 'xml'. Then try double-clicking as well to see how that works. Are you actually using SpreadSheetML or just simple xml? My results didn't improve in this respect until I started using SpreadSheetML (xlxml).

        Excel 2002 should recognize the .xml extension for it's file, while 2003 prefers .xlxml.

        Don
        WHITEPAGES.COM | INC
        Everything I've learned in life can be summed up in a small perl script!
Re: Openning XML in Excel using CGI
by rashley (Scribe) on Dec 05, 2006 at 22:27 UTC
    I still haven't figured this out.

    I have discoverred that the Content-Disposition and Content_size lines seem to be completely ignored, as commenting them out has no discernable effect.