<p>据我所知,Flask不支持<code>@app.route</code>url中的多个变量部分。但是,可以将文件的完整路径设置为变量节,然后对其进行解析以提取子文件夹路径和文件ID</p>
<p>下面是一个有效的例子:</p>
<pre><code>pathFits1 = [None] * 3
pathFits1[1] = 'content/images1/'
pathFits1[2] = 'content/images2/'
@app.route('/<path:fullpath>', methods=['GET'])
def returnImg(fullpath):
global pathFits1
print(" > GET request: ", fullpath)
# parse the full path to extract the subpath and ID
# subpath: get everything until the last slash
subpath = fullpath.rsplit('/', 1)[:-1][0] + '/'
# id: get string after the last slash
id = fullpath.rsplit('/', 1)[-1]
print("ID:", id, "\tsubpath:", subpath)
# try to send the file if the subpath is valid
if subpath in pathFits1:
print("valid path, attempting to sending file")
try:
return send_file(subpath + id, as_attachment=True, attachment_filename=id)
except Exception as e:
print(e)
return "file not found"
return "invalid path"
</code></pre>
<p>但是,更好的实现方式是将文件ID和路径作为GET请求参数(例如<code>http://127.0.0.1:8000/returnimg?id=3&path=content/images1/</code>)发送并按如下方式检索它们:</p>
^{pr2}$