doubipeng1336 2014-03-28 18:59
浏览 58
已采纳

Jquery使用rsync进度更新DIV

I'm trying to update a DIV with the ongoing output from an rsync command. The idea being I can see how the download is progressing.

This is the code I've got, but I'm getting errors relating 'Uncaught SyntaxError: Unexpected token ILLEGAL'

<?php
$down = popen('rsyncd -Pav http://ipv4.download.thinkbroadband.com/1GB.zip 1GB.zip', 'r');

while($progress = fgets($down, 124)) {
    ob_flush();flush();
?>
    <script type="text/javascript">
        $(document).ready(function() {
            var update = "<?php echo $progress; ?>";
            $("#status").html(update);
        });
    </script>

<?php
    ob_flush();
    ob_flush();flush();
}

    pclose($down);
}
?>

<div id="status"></div>

In the console I can see that it relates to :

var update = "    33390592  46%    4.62MB/s    0:00:08
    34701312  48%    4.63MB/s    0:00:07
    35979264  50%    4.63MB/s    0:00:07
";

How can I get each line of update and show it in a DIV without getting errors ?

Thanks

UPDATE

I'm now using this. The php echo for $update shows the output live in the web page, but the DIV is not updated until the page completes and then I only get the last line of output.

Why does the php echo work, but the jquery update to the div now work as expected ?

<?php
$down = popen('rsyncd -Pav http://ipv4.download.thinkbroadband.com/1GB.zip 1GB.zip', 'r');
$order   = array("
", "
", "");

while($progress = fgets($down, 32)) {
    ob_flush();flush();
    $update = str_replace($order,'<br />', $progress);
    echo $update; // <-- this outputs fine.
?>
    <script type="text/javascript">
        $(document).ready(function() {
            var update = "<?php echo $update; ?>";
            $("#status").html(update);
        });
    </script>

<?php
    ob_flush();flush();
}

    pclose($down);
}
?>

Is this due to the jquery not running until the page is fully loaded, so the div is only updated with the last entry of $update ?

If so is there any way to have the jquery run as the page is loading so the DIV is updated ?

Thanks :)

Thanks

UPDATE

<script type="text/javascript">
$(document).ready(function() {
    $.ajaxSetup({ cache: false });

    $("#test").click(function () {  $("#log").load("go.php"); });

});
</script>

Just tried the above, it works and calls go.php and I get the output but only when go.php has finished the rsync download. any way to show the ongoing output whilst the download happens ?

  • 写回答

1条回答 默认 最新

  • duanju9104 2014-03-28 19:04
    关注

    use str_replace() php function

    <?php
    $order   = array("
    ", "
    ", "");
    $replace = '<br />';
    ?>
    <script type="text/javascript">
                $(document).ready(function() {
                    var update = "<?php echo str_replace($order, $replace, $progress); ?>";
                    $("#status").html(update);
                });
            </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂