2015-06-23 88 views
0

我正在嘗試合併上傳一個基本的文本/ csv文件到我的web應用程序,該應用程序運行燒瓶來處理http請求。我試圖按照localhost here上運行的燒瓶文檔中的寶寶示例。但是,當我在我的頁面上嘗試這段代碼時,它似乎上傳,但後來只是掛起,事實上我的瓶服務器凍結,我不得​​不關閉終端再試一次... Ctrl + C甚至不工作。在python燒瓶中上傳文件

我執行run.py

#!/usr/bin/env python 
from app import app 

if __name__ == '__main__': 
    app.run(host='0.0.0.0', port=5000, debug=False, use_reloader=False) 

app是在同一目錄的目錄中run.py與以下__init__.py

import os 
from flask import Flask 
from werkzeug import secure_filename 

#Flask object initialization 
#app flask object has to be created before importing views below 
#because it calls "import app from app" 
UPLOAD_FOLDER = '/csv/upload' 
ALLOWED_EXTENSIONS = set(['txt', 'csv']) 

app = Flask(__name__) 
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER 

,這裏是我的views.py文件,其中有我的所有路由:

from flask import render_template, request, redirect, url_for 
from app import app 
import os 

#File extension checking 
def allowed_filename(filename): 
    return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS 

@app.route('/', methods=['GET', 'POST']) 
@app.route('/index.html', methods=['GET', 'POST']) 
def index(): 
    if request.method == 'POST': 
     submitted_file = request.files['file'] 
     if submitted_file and allowed_filename(submitted_file): 
      filename = secure_filename(submitted_file.filename) 
      submitted_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) 
      return redirect(url_for('uploaded_file', filename=filename)) 

    return ''' 
    <!doctype html> 
    <title>Upload new File</title> 
    <h1>Upload new File</h1> 
    <form action="" method=post enctype=multipart/form-data> 
     <p><input type=file name=file> 
     <input type=submit value=Upload> 
    </form> 
    ''' 
+0

有人可以告訴我字符串「uploaded_file」來自哪裏嗎? – computingfreak

回答

2

問題是你錯過了allowed_filename()。你應該通過submitted_file.filename而不是submitted_file本身

+0

哇,這是愚蠢的,謝謝。 – ministry