带有用于上传的 Flask 的 HTML 文件夹选择器

Inf*_*ty8 4 html python flask

我正在运行一个 Flask 应用程序,用户可以在其中上传文件,并且必须选择在网络驱动器上上传文件的位置的根文件夹路径。此路径是 IIS 可用的网络路径,也是所有用户计算机上的网络驱动器。

我想以 HTML 动态显示可用文件夹,即使在应用程序启动后创建了新文件夹。

我知道由于安全性,这不能用纯 HTML 来完成,但想知道 Flask 是否有办法解决这个问题。目标是使用 Python 将上传文件移动到选择的文件夹路径。

我试过了:

<form><input type="file" name=dir webkitdirectory directory multiple/></form>
Run Code Online (Sandbox Code Playgroud)

但这仅适用于 Chrome。通过用户选择的路径,我可以将其传递给 Python 以将上传文件复制到那里。

Inf*_*ty8 5

Due to modern browser limitations I decided to use JSTree as a solution. And it is working very well. It features a tree structure browser. The structure is the result of outputting the folders as JSON. You can add a search bar as well so the user can just type in a folder name to search.
Please see JSTree https://www.jstree.com/

How to implement this with Flask

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;
                            });
Run Code Online (Sandbox Code Playgroud)

In Flask/WTForms call on the id "folderout". This will return the path to WTForms when the user clicks the folder.

folderout = TextField('Folder:', validators=[validators.required()])
Run Code Online (Sandbox Code Playgroud)

To Create the JSON JStree File using Python:

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)
Run Code Online (Sandbox Code Playgroud)