开发者

Perl - Abort upload using CGI

开发者 https://www.devze.com 2023-03-23 19:44 出处:网络
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.

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.

Here's some 开发者_Python百科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 success, error, initializing, abort, or expired. When the status is abort, I would like to stop the upload completely.

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 -e $filepath 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.

Added some more code:

Perl:

my $cgi = CGI->new(\&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
        ... 
    }

} 

HTML:

<iframe id="upload_target" onload="uploadDone();"></iframe>

jQuery:

$('#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);
    }
}

Edit: 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: xhr.abort();


I don't know without sourcediving exactly how CGI is handling the upload and calling the hook function, but my first attempt to stop the upload would be to close STDIN.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号