Flask HTTP Server不允许一次上传多个文件

时间:2017-07-10 16:26:14

标签: python flask

import os
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename

UPLOAD_FOLDER = '/home/ubuntu/shared/'

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

@app.route("/", methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        file = request.files['file']
        if file:
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('index'))
    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" multiple="" name="file">
         <input type="submit" value="Upload">
    </form>
    <p>%s</p>
    """ % "<br>".join(os.listdir(app.config['UPLOAD_FOLDER'],))

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

如果我启动服务器并通过表单选择2个文件,它只会上传其中一个。我尝试了几个小时,阅读了大约15个主题,包括文档。 纳达:c

编辑: 我也尝试过改变:

        file = request.files['file']

成:

        file = request.files.getlist('file')

也不起作用。引号的类型也没有效果。那不是python3的东西吗?

1 个答案:

答案 0 :(得分:0)

import os, ssl
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename

UPLOAD_FOLDER = '/home/ubuntu/shared/'
certfile = "/home/ubuntu/keys/fullchain.pem"
keyfile = "/home/ubuntu/keys/privkey.pem"
ecdh_curve = "secp384r1"
cipherlist = "ECDHE-ECDSA-AES256-GCM-SHA384 ECDHE-ECDSA-CHACHA20-POLY1305"

sslcontext = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
sslcontext.options |= ssl.OP_NO_TLSv1
sslcontext.options |= ssl.OP_NO_TLSv1_1
sslcontext.protocol = ssl.PROTOCOL_TLSv1_2
sslcontext.set_ciphers(cipherlist)
sslcontext.set_ecdh_curve(ecdh_curve)
sslcontext.load_cert_chain(certfile, keyfile)

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

@app.route("/", methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        my_data = request.files.getlist('file')
        my_pass = request.form['password']
        if my_data and my_pass == 'yakumo':
            for file in my_data:
                my_handler(file)
            return redirect(url_for('index'))
    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 multiple name=file>
        <input type="password" name="password" value="">
        <input type=submit value=Upload>
    </form>
    <p>%s</p>
    """ % "<br>".join(os.listdir(app.config['UPLOAD_FOLDER'],))

def my_handler(f):
    filename = secure_filename(f.filename)
    f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8000, ssl_context=sslcontext, threaded=True, debug=False)

我犯了一个非常新的错误,并没有转发上传的多个文件。此处的代码经过测试,没有同时上传4个文件的问题。我希望它对某人有用。

编辑:使用一些甜蜜的TLS_1.2和密码字段更新了代码。享受相当安全的上传服务器。密码通过HTTPS传输。