Python Flask - 访问上传的文件

时间:2016-07-06 07:09:59

标签: python flask

请告知如何检索上传的上传文件。 这是我的代码:

import os
from flask import Flask, request, url_for, render_template
from werkzeug.utils import secure_filename

UPLOAD_FOLDER = '/user/static'  ## this is the folder on my machine
ALLOWED_EXTENSIONS = set(['txt', 'csv'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/upload', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        if 'file' not in request.files:
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return filename 
    return render_template('upload.html')

@app.route('/report', methods=['GET', 'POST'])
def get_file():
    # need to retrieve the uploaded file here for further processing
    return render_template('report.html')

例如,上传的文件名为example.csv

提前谢谢

0 个答案:

没有答案