我正在尝试在我的Flask服务器中创建一个简单的路由,在这里只要访问URL就可以下载一个文件。这是我的烧瓶代码:
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static/')
app = Flask(__name__, static_url_path=UPLOAD_FOLDER)
Bootstrap(app)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.debug = False
app.secret_key = 'notverysecret'
@app.route('/', methods=['GET', 'POST'])
def index():
...
return render_template('index.html', request="POST", pitches=pitches)
@app.route('/mxl/')
def mxl():
return app.send_static_file(UPLOAD_FOLDER + 'piece.mxl')
if __name__ == "__main__":
app.run()
但是,当我访问localhost:5000/mxl/
或localhost:500/mxl
时,我得到一个“请求的URL在服务器上找不到。如果您手动输入URL,请检查拼写并重试。“错误。在我的命令行中我看到:
为什么会这样?在
当我运行app.url_map
时,我得到以下输出:
Map([<Rule '/mxl/' (HEAD, OPTIONS, GET) -> mxl>,
<Rule '/' (HEAD, POST, OPTIONS, GET) -> index>,
<Rule '/home/myusername/guitartab/guitartab/static//bootstrap/<filename>' (HEAD, OPTIONS, GET) -> bootstrap.static>,
<Rule '/home/myusername/guitartab/guitartab/static//<filename>' (HEAD, OPTIONS, GET) -> static>])
问题是Flask有一个参数
static_folder
,它默认为应用程序根路径中的“static”文件夹。去看医生知道send_static_file(filename)。在只需将代码更改为:
如果要包含路径,请使用send_from_directory:
^{pr2}$当您浏览
http://127.0.0.1:5000/mxl/
时,它将下载文件piece.mxl
相关问题 更多 >
编程相关推荐