2016-04-27 103 views
1

我有一個flask(0.10.1)應用程序在Debian Jessie VPS上運行,並由nginx(1.6.2)驅動。該應用程序工作正常,但我最近添加了一個特定的route問題。燒瓶路線下載.xml文件導致404未找到

route用於下載.xml文件。

它是動態的,告訴的目錄和文件名:

@app.route('/backups/<dir_key>/<filename>') 

而且它註冊基礎上,flasksend_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) 

生成路由感謝flaskurl_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> 

但是當我按一下按鈕,我收到以下錯誤:

enter image description here

這是什麼奇怪的是:

  1. 當應用程序由本地Windows機器上的小型Python服務器供電時,正確觸發下載。
  2. 我有一個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文件和工作正常。

回答

0

您是否記得在服務器上重新加載應用程序?如果我在開發計算機和Web服務器上獲得不同的結果,那通常就是問題所在。

例如,如果您使用gunicorn進行部署,則必須重新啓動gunicorn,以便服務器知道您的代碼更改。

+0

謝謝。是的,我重新啓動了應用程序。 –