由於現代瀏覽器的限制,我決定使用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