The correct solution here is to use the HTTP status code. This is why HTTP responses include a status code.

If the file exists, the server returns 200 OK and the file as application/zip. If the file does not exist, the server returns 404 Not Found. The client then distinguishes based on this.status: 200 for a BLOB is included and 404 for an error.

Here are the relevant bits of the revised JavaScript: (obviously untested)

xhttp.onreadystatechange = function() { if (this.readystate == 4) { if (this.status == 200) { if (xhttp.responseType == 'blob') { // existing goofy file download code here } else { // report internal error } } else if (this.status = 404) { // report error here } } }

As for the Perl code on the server side, you did not provide a SSCCE, so I can give only a broad outline of how the server processing should work:

  1. [whatever initial setup is needed]
  2. Read the form data from the client.
  3. Determine if the requirements to download a file have been met, and if so, which file is to be downloaded.
  4. Produce a reply, with either a 200 OK status if presenting a file, or 404 Not Found otherwise.

In reply to Re: Sending file as "TEXT" or "BLOB" by jcb
in thread Sending file as "TEXT" or "BLOB" by cristofayre

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.