我有一個flask
(0.10.1)應用程序在Debian Jessie VPS
上運行,並由nginx
(1.6.2)驅動。該應用程序工作正常,但我最近添加了一個特定的route
問題。燒瓶路線下載.xml文件導致404未找到
route
用於下載.xml
文件。
它是動態的,告訴的目錄和文件名:
@app.route('/backups/<dir_key>/<filename>')
而且它註冊基礎上,flask
send_from_directory
功能的函數:
def backups(dir_key,filename):
directory = os.path.join(app.config['BACKUPXML_FOLDER'], dir_key)
return send_from_directory(directory, filename, as_attachment=True)
生成路由感謝flask
url_for
功能,並返回到前端:
return jsonify({
'backupFileUrl': url_for('backups', dir_key=dir_key, filename = filename, _external=True)
})
它存儲在AngularJS
變量:
$scope.backupFileUrl = response.backupFileUrl;
最後包括在<a>
標籤下載:
<a class="btn btn-primary"
ng-show="sessionDownload"
ng-href="{{ backupFileUrl }}" target="_blank">
<span class="glyphicon glyphicon-save"></span> Télécharger </a>
但是當我按一下按鈕,我收到以下錯誤:
這是什麼奇怪的是:
- 當應用程序由本地
Windows
機器上的小型Python
服務器供電時,正確觸發下載。 - 我有一個
route
打算下載.xlsx
文件這是實際工作,並在本地Windows
機器和Jessie VPS
。
有人看到我如何定義route
使它工作?
這裏是API體系結構如果需要的話:
API/app.py
import sys
sys.path.append('../')
from flask_script import Server, Manager
from kosapp import app, db
manager = Manager(app)
if __name__ == '__main__':
manager.run()
API/config.py
from os.path import abspath, dirname, join
import tempfile
basedir = dirname(abspath(__file__))
BASEDIR = dirname(abspath(__file__))
DEBUG = True
REPORTS_FOLDER = '/tmp/reports'
# on local machine
# REPORTS_FOLDER = os.path.join(tempfile.gettempdir(), 'reports')
BACKUPXML_FOLDER = '/tmp/backups'
# on local machine
# BACKUPXML_FOLDER = os.path.join(tempfile.gettempdir(), 'backups')
API/kosapp/__ init__ .py
from flask import Flask
app = Flask(__name__)
app.url_map.strict_slashes = False
app.config.from_object('config')
from kosapp import views
api/kosapp/views。PY
import os
from flask import send_file, jsonify, request, render_template, send_from_directory
from kosapp import app
@app.route('/reports/<dir_key>/<filename>')
def reports(dir_key, filename):
directory = os.path.join(app.config['REPORTS_FOLDER'], dir_key)
return send_from_directory(directory, filename)
@app.route('/backups/<dir_key>/<filename>')
def backups(dir_key,filename):
directory = os.path.join(app.config['BACKUPXML_FOLDER'], dir_key)
return send_from_directory(directory, filename, as_attachment=True)
作爲一個說明,路線'/reports/<dir_key>/<filename>'
是用於下載.xlsx
文件和工作正常。
爲什麼不讓Nginx爲您提供文件? – dirn
你的意思是直接指向''標籤中的文件嗎? –
您正在使用Flask來從特定文件夾提供文件。 Nginx會做得更好。您可以設置別名或位置以匹配您需要匹配的前綴。 – dirn