perl异步请求ajax,javascript - jQuery和Perl:基于“管道文件”,动态ajax状态的进度条 - 堆栈内存溢出...

自从我问这个问题以来已经快一个月了,但是没有出现答案。 因此,我现在发布基于ThisSuitIsBlackNot的评论的评论。

尽管没有详细说明,但它可以作为有关如何连接Perl,HTML,Javascript / Ajax和JSON的最小示例。 也许它可以帮助某人开始使用该主题。

如果要运行此代码,只需将index.html文件复制到html目录(例如/ var / www / html),然后将perl脚本复制到cgi-bin目录(例如/ var / www / cgi-bin)。 确保使这些perl脚本可执行! 在下面的代码中,cgi目录位于/ var / www / cgi-bin / ajax / stackCGI中-请相应地进行更改。

将管道的状态写入文件,然后以1秒的间隔读取该文件,更新进度条,并显示有关当前状态的消息。 Perl的sleep函数表示流水线中单个步骤所花费的持续时间。

文件如下。

欢迎任何评论和改进!

index.html的:

Test

.ui-progressbar {

position: relative;

}

.progress-label {

position: absolute;

left: 50%;

font-weight: bold;

text-shadow: 1px 1px 0 #fff;

}

var progressVal = 0;

function get_progress() //get_progress();

{

$.ajax({

type: 'POST',

url: '/cgi-bin/ajax/stackCGI/readFromFileJson.pl',

success: function( res ) {

$('#progressPerc').append(' ' + res.progressSummary.progress);

$('#progressMessage').html('status of pipeline: ' + res.progressSummary.progressMessage);

$('.progress-label').html(res.progressSummary.progress + '%');

progressVal = parseFloat(res.progressSummary.progress);

$( "#progressbar" ).progressbar({

value: progressVal

});

}

});

if (progressVal < 100){ //pipeline has not finished yet

setTimeout(get_progress, 1000); //call the function each second every second to get a status update

}

else { //pipeline has finished

$('.progress-label').html('100%');

alert("pipeline has finished! your results can be found in path/to/files. an e-mail has been sent to user@provider.com");

}

}

function start_pipeline()

{

$.ajax({

type: 'POST',

url: '/cgi-bin/ajax/stackCGI/pipeline.pl',

data: { 'fileAnalysis': $('#myFile').val() },

success: function(res) {

//add your success function here

},

error: function() {alert("pipeline has not started!");}

});

}

file name:

Analyze now

status of pipeline in percent (in this example the function get_progress is called every second):

pipeline.pl:

#!/usr/bin/perl

use strict;

use warnings;

use CGI;

my $q = new CGI;

print $q->header('text/plain'); #needed! otherwise the ajax call in start_pipeline returns the error message

my $fileForAnalysis = $q -> param('fileAnalysis');

#create a file where the progress is reported to

#make sure you have the appropriate permissions to do this

my $filename = '/var/www/cgi-bin/ajax/stackCGI/progressReport.txt'; #change the directory!

my $fh; #file handler

my $number; #progress of pipeline in percent

my $message; #progress of pipeline

$number = 0;

$message = 'pipeline has startet successfully! Your file '.$fileForAnalysis.' is now processed.';

open($fh, '>', $filename) or die "Could not open file '$filename' $!";

print $fh $number."\t".$message;

close $fh;

sleep(3); #first program is running

$number = 10; #progress of pipeline in percent. as we have 4 programs in this pipeline it could also be 25 or whatever

$message = 'first program has finished';

open($fh, '>', $filename) or die "Could not open file '$filename' $!";

print $fh $number."\t".$message;

close $fh;

sleep(5); #second program is running

$number = 20;

$message = 'second program has finished';

open($fh, '>', $filename) or die "Could not open file '$filename' $!";

print $fh $number."\t".$message;

close $fh;

sleep(5); #third program is running

$number = 42;

$message = 'third program has finished';

open($fh, '>', $filename) or die "Could not open file '$filename' $!";

print $fh $number."\t".$message;

close $fh;

sleep(5); #fourth program is running

$number = 100;

$message = 'pipeline has finished';

open($fh, '>', $filename) or die "Could not open file '$filename' $!";

print $fh $number."\t".$message;

close $fh;

readFromFileJson.pl:

#!/usr/bin/perl

use strict;

use warnings;

use JSON;

use CGI;

my $q = new CGI;

#create a file where the progress is reported to

#make sure you have the appropriate permissions to do this

my $filename = '/var/www/cgi-bin/ajax/stackCGI/progressReport.txt'; #change the directory!

open(my $fh, '<:encoding or die not open file>

print $q->header('application/json;charset=UTF-8'); #output will be returned in JSON format

my @progressReport = split(/\t/,); #file is tab separated

my %progressHash;

$progressHash{"progress"} = $progressReport[0];

$progressHash{"progressMessage"} = $progressReport[1];

#convert hash to JSON format

my $op = JSON -> new -> utf8 -> pretty(1);

my $output = $op -> encode({

progressSummary => \%progressHash

});

print $output;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值