<input type="file" name="client-file" id="get-files" multiple />
<script>
var inp = document.getElementById("get-files");
// Access and handle the files
for (i = 0; i < inp.files.length; i++) {
let file = inp.files[i];
// do things with file
}
</script>
var directory = <path>;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', directory, false); // false for synchronous request
xmlHttp.send(null);
var ret = xmlHttp.responseText;
var fileList = ret.split('\n');
for (i = 0; i < fileList.length; i++) {
var fileinfo = fileList[i].split(' ');
if (fileinfo[0] == '201:') {
document.write(fileinfo[1] + "<br>");
document.write('<img src=\"' + directory + fileinfo[1] + '\"/>');
}
}
<script>
/* this function will be fired when there are files
in dir search in php' glob
*/
function callback(files) {
alert(files);
}
/* call inside document.ready to make sure the
callback is already loaded
*/
$(document).ready(function() {
let php = document.createElement("script");
php.setAttribute("src", "/dir.php");
document.body.appendChild(php);
});
</script>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "/assets/photos/", true);
xhttp.send();
}
function myFunction(xml) {
// console.log(xml.responseText)
var parser = new DOMParser();
var htmlDoc = parser.parseFromString(xml.responseText, 'text/html');
var preList = htmlDoc.getElementsByTagName("pre")[0].getElementsByTagName("a")
for (i = 1; i < preList.length; i++) {
console.log(preList[i].innerHTML)
}
}
</script>
get_file(conf.data_address).then(imgs => { // get_file is wrapper of fetch API
let img = new Image();
img.src = `./files/Blackpink/${imgs[0]}`; // e.g. load the first img
img.height = 40;
return new Promise(resolve => {
img.onload = () => {
resolve(img);
}
})
})
服务器端的 django:
def get(self, request): # get method
address = request.query_params.get('address') # get the requested folder
is_database = request.query_params.get('is_database')
if address.endswith('/'):
j = u.get_path_files(address,is_full=False) # wrapper of os.listdir of python to get the files in the requested directory.