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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探