doupuxuan5784 2019-05-20 09:56
浏览 79

如何在ajax请求后有效地呈现文件

For a filesystem, I use an ajax request to rename a directory or file.

Scanning files in a directory:

$files = array_diff( scandir($dir), array(".", "..", "tmp") );

Part of the php code:

if( isset($_POST['new_name']) ){        

// code for renaming goes here
    echo $_POST['old_name'] . 'renamed to ' . $_POST['new_name'];
exit;
}

For renaming a directory or file, show filename, date, extension and so on..., I use a foreach and the structure looks like below:

<div class="echo"><div>
<div class="dynamic-content">

foreach($files as $file) {

?>
 <!-- Rename-->     
<td class="td-rename">
  <h4>Rename File/Folder</h4>
  Current name: <b><?php echo basename($dir.'/'.$file); ?></b><br />
  <form class="rafform" method="post">
    <input type="hidden" class="old_name" name="old_name" value="<?php echo $dir . '/' . $file; ?>" />                                      
    <input  type="text" class="new_name form-control" name="new_name" placeholder="only a-z, A-Z, 0-9, -, _" value="" />
    <input type="submit" class="submitmodal rename btn " value="Rename" />                      
  </form>
</td>

<!-- below are coming more `td's` like:-->
<td class="td-filename">
…………
</td>    
<td class="td-date">
…………
</td>
<td class="td-extension">
…………
</td>
and so on...
<?php } ?> // end foreach
</div> <!-- end dynamic content-->

My ajax request looks like this:

$(document).on('submit' , ".rafform" , (function(e) {

e.preventDefault();

$.ajax({
url: "",
type: "post",
data:  new FormData(this),
cache: false,               
contentType: false,
processData: false,             
beforeSend: function () {
    window.location.hash='close'; /* show spinner and disappear upload modal */                 
    },
success: function(data) {

    $('.dynamic-content').load('index.php .dynamic-content > *');
    $('.echo').append(data);
    $('.uploadspinner').hide();                 
    $(".echolist").delay(5000).fadeOut(500);

},


});

}));

So after each request: the echo from PHP is put in the div with class echo and to see the changes immediately, I do a load() of the same page index.php with only the content in the div with class dynamic-content. Because in this div, the files are rendered. The user can see immediately that the file is renamed.

But now I notice, doing it this way, when a directory has a lot of subdirectories and files, rendering the files is getting slower and slower.

Is there a faster way to render the files after an change (rename per example)? For me it is important that every change is immediately visible for the user! And if so, what will be the structure I have to use?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?