由于现代浏览器的限制,我决定使用JSTree作为解决方案。它工作得很好。它具有一个树形结构浏览器。该结构是将文件夹输出为JSON的结果。您也可以添加搜索栏,以便用户只需键入要搜索的文件夹名称即可。
请参阅JSTree https://www.jstree.com/
如何用瓶
HTML/JS实现这一点:
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css">
<div>
<input class="search-input form-control" placeholder="Search for folder"></input>
</div>
<script id="jstree1" name="jstree1">
/*Search and JS Folder Tree*/
$(function() {
$(".search-input").keyup(function() {
var searchString = $(this).val();
console.log(searchString);
$('#container').jstree('search', searchString);
});
$('#container').jstree({
'core': {
"themes": {
"name": "default"
, "dots": true
, "icons": true
}
, 'data': {
'url': "static/JSONData.json"
, 'type': 'GET'
, 'dataType': 'JSON'
}
}
, "search": {
"case_insensitive": true
, "show_only_matches": true
}
, "plugins": ["search"]
});
});
{ /* --- THIS IS FOLDER SELECTOR FOR ID "folderout" --- */
$("#container").on("select_node.jstree", function (evt, data) {
var number = data.node.text
document.getElementById("folderout").value = number;
});
在瓶/ WTForms上的ID “folderout”电话。这将在用户单击该文件夹时返回到WTForms的路径。
folderout = TextField('Folder:', validators=[validators.required()])
创建使用Python的JSON JStree文件:
import os
# path : string to relative or absolute path to be queried
# subdirs: tuple or list containing all names of subfolders that need to be
# present in the directory
def all_dirs_with_subdirs(path, subdirs):
# make sure no relative paths are returned, can be omitted
path = os.path.abspath(path)
result = []
for root, dirs, files in os.walk(path):
if all(subdir in dirs for subdir in subdirs):
result.append(root)
return result
def get_directory_listing(path):
output = {}
output["text"] = path.decode('latin1')
output["type"] = "directory"
output["children"] = all_dirs_with_subdirs(path, ('Maps', 'Reports'))
return output
with open('test.json', 'w+') as f:
listing = get_directory_listing(".")
json.dump(listing, f)
当然,列举在这里用户可以上传文件,将它们发送到客户端的地方,让他们挑一个。然后使用它。你有什么具体问题? – davidism
需要这是动态的。可用路径将随着新文件夹的创建而改变。 – Infinity8
好的,那么,你有什么具体问题?用户每次请求该页面时,都列举当前可用的文件夹。请阅读[问]和[mcve],你的问题缺乏细节。 – davidism