This is partially PERL and partially javascript (Sorry)

Basically, I want to send an HTTPRequest to the server. If the file =DOES NOT= exist, it throws back an error as a TEXT response. If it DOES exist, it passes back the file as a BLOB. (Not really sure how to send a file as a BLOB from PERL - which is the point of this question)

** Using "Content-type: attachment; application/zip" and "content-disposition" to invoke the save dialouge I can just about understand ... but that will not report an error if the file doesn't exist. (The file is accessed by user entering a code number into a form, so a wrong number needs to throw an error warning)

Here is the basic code:

CLIENT SIDE: function downFile(){ var formData = new FormData(); formData.set('email','$email'); formData.set('gf_line','$line'); formData.set('code','$code'); var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4){ if (this.status == 200) { if (xhttp.responseType == 'text' || xhttp.responseType == +'') { alert('Sorry, but there appears to be an error\\nThis coul +d be:\\n\\n- The email address entered\\ndoes not match the email in +database\\n\\n- The code you entered is incorrect\\n\\nPlease check y +our details, and try again'); window.history.back(); return false; } else if (xhttp.responseType == 'blob'){ // Create a new Blob object using the response data of the + onload object var blob = new Blob([this.response], {type: 'application/z +ip'}); //Create a link element, hide it, direct it towards the bl +ob, and then 'click' it programatically let a = document.createElement("a"); a.style = "display: none"; document.body.appendChild(a); //Create a DOMString representing the blob and point the l +ink element towards it let url = window.URL.createObjectURL(blob); a.href = url; a.download = 'Branded Files.zip'; //programatically click the link to trigger the download a.click(); //release the reference to the file by revoking the Object + URL window.URL.revokeObjectURL(url); } } } xhttp.open("POST", "download.pl", true); xhttp.send(formData); } </script> SERVER SIDE: ... .. . if ($inEmail ne $email){ print "content-type: text/html\n\n"; print "500"; # Returned as a "TEXT" response exit; } else{ open FILE, "<", "../data/$user/$dir/$code/$fileCode.zip"; binmode FILE; while(<FILE>){ $file.=$_; } close FILE; #print "content-type: text/html\n\n"; #print "content-type: application/zip\n"; #print "content-disposition: attachment; filename='Branded Files'\n\n" +; print $file; # Returned as a "BLOB" response

Both responses return as text

The theory is that the code in the else statement will create a fake anchor, load the blob into it, emulate a click and thus invoke the "SAVE" dialogue. Admit this is "copy / paste" so - whilst I understand what it's doing - it's not my code

In a nutshell, I want an alert if the file does not exist, or a SAVE dialogue if it DOES exist, ideally without navigating away from the page. There may be an easier way to do it, so open to suggestions


In reply to 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.