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:
- [whatever initial setup is needed]
- Read the form data from the client.
- Determine if the requirements to download a file have been met, and if so, which file is to be downloaded.
- Produce a reply, with either a 200 OK status if presenting a file, or 404 Not Found otherwise.