title: Perl - Abort upload using CGI
body: <p>I am writing a file management application in Perl, PHP, and JavaScript (jQuery) and I would like to give the user the ability to cancel their upload. </p>
  
  <p>Here's some background infos: 
  When the user chooses to upload a file, I change the target of the form to an iFrame, which posts to the perl script. Within the script, I update a row in the database that shows the current status of the upload. The status could be <code>success</code>, <code>error</code>, <code>initializing</code>, <code>abort</code>, or <code>expired</code>. When the status is <code>abort</code>, I would like to stop the upload completely. </p>
  
  <p>Is there a way to kill off this script from within the hook? (So that I don't have to wait for the whole file.) On a side note, the <code>-e $filepath</code> works, but it still loops through the whole file, so the message won't display until afterwards. In either case, I would just like the script to completely stop.</p>
  
  <p><strong>Added some more code:</strong></p>
  
  <p>Perl: </p>
  
  <pre><code>my $cgi = CGI-&gt;new(\&amp;hook, \%data, 0);
  
  sub hook { 
      my $status = GetStatus();
  
      if(-e $$data{filepath} and $status eq "initializing"){
          # send JSON back to browser
          # insert your amazing snippet here
          ... 
      }
  
      if($status eq "success"){
          # continue writing file to disk
          # update db
          ... 
      }
  
      if($status eq "abort"){
          # cancel the upload
          # insert your amazing snippet here
          ... 
      }
  
  } 
  </code></pre>
  
  <p>HTML:</p>
  
  <pre><code>&lt;iframe id="upload_target" onload="uploadDone();"&gt;&lt;/iframe&gt;
  </code></pre>
  
  <p>jQuery:</p>
  
  <pre><code>$('#upload_form').submit(function(data){
      window.setInterval('doProgress()', 250);
  });
  
  function doProgress() {
      $.ajax({
          ...
          success: function(data){
              json = $.parseJSON(data);
              if(json.status != null){
                  // json.message contains the percentage number
                  $("#progressbar").reportprogress(json.message);
              }
          }
      });
  }
  
  function uploadDone() { 
      var ret = $('#upload_target').contents().find('body').text();
      var json = jQuery.parseJSON(ret);
  
      if(json.status != null){
          $.modal.close();
          show_message(json);
      }
  }
  </code></pre>
  
  <p><strong>Edit:</strong>
  I saw somewhere on Google that aborting the request through javascript might be the way to solve the problem. Can anyone say for sure whether this is true? What it entails is simply assigning the request to a variable, then calling the abort() method on it, ex: <code>xhr.abort();</code></p>