Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Flask-访问上载的文件_Python_Flask - Fatal编程技术网

Python Flask-访问上载的文件

Python Flask-访问上载的文件,python,flask,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

请就如何检索上传的文件提供建议。 这是我的密码:

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

先谢谢你


Joe

向报告路由发送一个参数,以便它知道要检索的文件,然后使用file=open('static_folder'+filename')打开。read()感谢GMarsh。我会尽力解决的。谢谢大家