在两个不同的字段上传两个不同的文件

时间:2016-01-24 01:07:39

标签: python flask

我正在使用flask来开发webapp,我有两个不同的文件可以上传到两个不同的位置,就我在互联网上搜索而言,我只找到在一个文件字段中将一个或多个一起上传的说明工作正常。

enter image description here

这是我用来上传一个文件的代码:

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

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

@app.route("/", methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        file = request.files['file']
        if file > 0:
            filename = secure_filename(file.filename)
            file.save(os.path.join(UPLOAD_FOLDER , filename))
            return redirect(url_for('index'))

有人可以帮助我吗

2 个答案:

答案 0 :(得分:1)

输入的名称将是您从SP_DEVICE_INTERFACE_DETAIL_DATA

检索文件的密钥

HTML:

request.files

烧瓶中:

<input type="file" name="first_file" />
<input type="file" name="second_file" />

Documentation.

答案 1 :(得分:0)

这是整个代码:

import os
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename
import sys
sys.path.insert(0, '/home/muteb/Desktop/test')

UPLOAD_FOLDER = '/home/muteb/Desktop/test/'


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


@app.route("/", methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        file1 = request.files['file1']
        file2 = request.files['file2']
        if file1 > 0:
            filename1 = secure_filename(file1.filename)
            file1.save(os.path.join(UPLOAD_FOLDER , filename1))
        if file2 > 0:
            filename2 = secure_filename(file2.filename)
            file2.save(os.path.join(UPLOAD_FOLDER , filename2))
            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 name=file1>
          <input type=file name=file2>
         <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=5002, debug=True)