2016-09-08 45 views
0

我想要獲取稍後將存儲到數組中的文件名列表。以下代碼只是以與瀏覽器中顯示的Apache列表相同的方式顯示目錄列表。 G。 1234.txt 31-Aug-2016 13:17 35K如何改變它,所以我只得到文件名?獲取給定目錄中的文件名jQuery

<script type="text/javascript"> 
$(document).ready(function() { 
    $.get("dat/", function(data) { 
    $("#files").append(data); 
    }); 
}); 
</script> 
<body> 
<div id='files'></div> 
</body> 

回答

3

請用下面的代碼嘗試。 openFile函數可用於檢查它是文件還是文件夾。根據您的使用情況,請添加更多功能擴展。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> 
<script> 
    var fileNames = new Array(); 
    $.ajax({ 
     url: "/test/", 
     success: function(data){ 
     $(data).find("td > a").each(function(){ 
      if(openFile($(this).attr("href"))){ 
       fileNames.push($(this).attr("href")); 
      }   
     }); 
     } 
    }); 
    console.log(fileNames); 
    function openFile(file) { 
     var extension = file.substr((file.lastIndexOf('.') +1)); 
     switch(extension) { 
      case 'jpg': 
      case 'png': 
      case 'gif': // the alert ended with pdf instead of gif. 
      case 'zip': 
      case 'rar': 
      case 'pdf': 
      case 'php': 
      case 'doc': 
      case 'docx': 
      case 'xls': 
      case 'xlsx': 
       return true; 
       break; 
      default: 
       return false; 
     } 
    }; 
</script> 
相關問題