2011-12-09 5 views
0

다운로드 할 파일 목록에 대한 웹 페이지를 작성하려고합니다. 파일은 웹 페이지와 함께 저장되며 웹 페이지에서 다운로드 할 폴더의 모든 파일을 동적으로 나열하려고합니다. 그렇게하면 웹 페이지를 수정하지 않아도됩니다. 자바 스크립트를 사용하여 웹 페이지에서 링크를 만드는 방법을 알고 있지만 먼저 파일의 이름을 찾아 사용해야합니다.JavaScript로 웹 사이트에 저장된 다른 파일에 어떻게 액세스합니까?

파일 브라우저와 같은 파일을 탐색하는 코드가 있지만 현재 위치를 저장하는 문자열 만 사용하는 웹 사이트를 발견했습니다.

이 헤더에 있습니다

<script type="text/javascript"><!-- 

var myloc = window.location.href; 
var locarray = myloc.split("/"); 
delete locarray[(locarray.length-1)]; 
var fileref = locarray.join("/"); 

//--></script> 

이 몸에 :

<form> 
<input type=button value="Show Files" onClick="window.location=fileref;"> 
</form> 

그러나이 정말 아닌이 파일에 대한 다운로드 링크를 만들려고하고 있기 때문에 도움이되지 않습니다 파일 탐색기.

편집 :

당신은 기존의 HTML 페이지를 호스팅 할 때 당신은 당신이 사용하는 무엇을 적 서버 페이지의 htmlfile 및 이미지 나 컨텐츠를 업로드 할 수 있습니다. 자바 스크립트를 사용하여 웹 페이지에서 호스팅되는 모든 파일에 동적으로 링크하고 싶습니다. 파일을 사용할 수있게 만드는 간단한 방법으로 Dropbox 공용 폴더에 파일을 호스팅하는 데이 작업을 결합하려고합니다.

+0

정확히 무엇을 요구하고 있습니까? JavaScript의 폴더에있는 모든 파일의 목록을 만들고 싶습니까? – Blender

답변

1

서버에있는 파일의 목록을 원하는 경우에 당신이 그들의 이름을 수집하기 위해 서버 측 스크립트를 사용해야합니다 :

JS--

//use AJAX to get the list of files from a server-side script 
$.getJSON('path/to/server-side.php', { 'get_list' : 'true' }, function (serverResponse) { 

    //check the response to make sure it's a success 
    if (serverResponse.status == 'success') { 
     var len = serverResponse.output.length, 
      out = []; 

     //iterate through the serverResponse variable 
     for (var i = 0; i < len; i++) { 

      //add output to the `out` variable 
      out.push('<li>' + serverResponse.output[i] + '</li>'); 
     } 

     //place new serverResponse output into DOM 
     $('#my-link-container').html('<ul>' + out.join('') + '</ul>'); 
    } else { 
     alert('An Error Occured'); 
    } 
}); 

PHP--

<?php 

//check to make sure the `get_list` GET variable exists 
if (isset($_GET['get_list'])) { 

    //open the directory you want to use for your downloads 
    $handle = opendir('path/to/directory'); 
    $output = array(); 

    //iterate through the files in this directory 
    while ($file = readdir($handle)) { 

     //only add the file to the output if it is not in a black-list 
     if (!in_array($file, array('.', '..', 'error_log'))) { 
      $output[] = $file; 
     } 
    } 
    if (!empty($output)) { 

     //if there are files found then output them as JSON 
     echo json_encode(array('status' => 'success', 'output' => $output)); 
    } else { 

     //if no files are found then output an error msg in JSON 
     echo json_encode(array('status' => 'error', 'output' => array())); 
    } 
} else { 

    //if no `get_list` GET variable is found then output an error in JSON 
    echo json_encode(array('status' => 'error', 'output' => array())); 
} 
?> 
관련 문제