Dear Monks,

I have an odd thing happening that I've been trying to fix for two days. I have a script on a server that generates an Excel file using Excel::Writer::XLSX to create a file with an extension of .xlsx. It creates an excel file that, if I download with FTP, opens properly and with no issues.

The problem is that the user won't have FTP access and will need to use a download link. As the file is written to a location outside of normal web space, I have a script that facilitates the download. I've been using it with text files and there is no issue.

I've also used this same format in the past for other binary files (JPG, PDF, Word, etc) and have not had a problem.

What is happening is that when I click on the downloaded file to open it I get first a message that says:
"We found a problem with some content in FILENAME. Do you want us to try recover as much as we can? If you trust the source of the workbook, click Yes."

When I click YES I get the message that says it was trying to open and repair the file, but nothing happens.

I know the file is okay because I can retrieve it over FTP and it opens fine. So the problem must be in the download itself.

I'm using the mime-type "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" and I've also tried "application/vnd.ms-excel"

Here is the portion of the code that does the download. (It is used for both binary and text files.)

if(open(FH, "<$filepath")) { print "Content-Type:$mimetype; name = \"$filename\"\n"; print "Content-Disposition: attachment; filename = \"$filename\"\n +\n"; while( my $line = <FH>) { #I am chomping then adding new lines because the UNIX line fee +ds are not #recognized on some winderz computerz apps. This is why I stri +p the UNIX line #feed (\n) and then add a Windows line feed (\r\n) if($mimetype eq 'text/plain') { chomp($line); print $line, "\r\n"; } else { print $line; } } close(FH); print "EOF\n\n"; exit; } else { &error_page; }

Server Software: LiteSpeed
Operating System: Linux
Perl version: 5.016003

I've tried downloading this on a 2-year old Windows11 operating system in Chrome version 109.
I've tried downloading this on a 10-year old laptop with Windows 8 operating system in Chrome version 109.
I've also tried downloading from Microsoft Edge 109, Firefox 86

I'm really bugged by this (pun intended) because I've done this same thing with many file types in the past (but never Excel) and never had a problem, binary or text.

Does anyone have experience with this problem and know what it is I'm doing wrong?

Thank you for your time and wisdom.


In reply to Download from Perl script corrupted for XLSX file. by TorontoJim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.