使用 bottle py 使文件可在 working/project 目录外下载

Make file available for download outside working/project directory with bottle py

当文件位于您的文件系统根目录中时,如何使文件在 bottle 中可供下载。例如,在 ubuntu 操作系统的 'media' 目录中?

python:

#bottle 0.12.10
@route('/download/<filename:path>')
def staticdownloadfile(filename):
    return bottle.static_file(filename, root='/media', download=filename)

html:

<a href="/static/media/somefile.extension">download here</a>

以上方法行不通? 404 未找到。

在 html 文件中你应该使用另一个 link:

<a href="/download/somefile.extension">download here</a>

因为您在应用中定义了:

@route('/download/<filename:path>')