Can onprogress functionality be added to jQuery.ajax() by using xhrFields?

前端 未结 3 1163
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 02:42

As suggested here: https://gist.github.com/HenrikJoreteg/2502497, I\'m trying to add onprogress functionality to my jQuery.ajax() file upload. The upload works

3条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 03:00

    This is a bit of a hack of another answer elsewhere on Stack Overflow, but I think answers your question. I have doctored it to my own needs of pumping "streamed" data from the server to a scrollable div (which I can then force to always scroll to the bottom thus showing progress over time and thus not waiting for the entire record set to complete).

    The client side code below adds the resulting content to a predefined div with id "scrollable_area" (which can then be scrolled)...

    The server side "your_api_call.php" file call would need to then flush its output (per data row so as to see progress over time) which can then be displayed immediately within the above "scrollable_area" div...

    // Do Db loop
            while ($record = $recordset->fetch(PDO::FETCH_ASSOC)) {
                set_time_limit(10); // Don't timeout on large data sets seeing as this is a big task that we are wanting to watch progress!
                echo 'Do what you gotta do... ' . $record["register_id"] . '
    '; flush(); // Push to the client / ajax ob_flush(); // As above }

    Short answer... YES. Hope this helps :-)

提交回复
热议问题