how to upload multiple files using flask in python
Question: Here is my code for multiple files upload:

**HTML CODE:**

    
    
    Browse <input type="file" name="pro_attachment1" id="pro_attachment1" multiple>
    

**PYTHON CODE:**

    
    
    pro_attachment = request.files.getlist('pro_attachment1')
    
    for upload in pro_attachment:
        filename = upload.filename.rsplit("/")[0]
        destination = os.path.join(application.config['UPLOAD_FOLDER'], filename)
        print "Accept incoming file:", filename
        print "Save it to:", destination
        upload.save(destination)
    

But it uploads a single file instead of multiple files.


Answer: You can use [Flask-Uploads](https://pythonhosted.org/Flask-Uploads/).  
Here is a image(multiple) upload demo.

View:

    
    
    from flask_uploads import UploadSet, configure_uploads, IMAGES
    from flask import Flask, request, render_template, flash
    app = Flask(__name__)  
    app.config['UPLOADED_PHOTOS_DEST'] = '\static\img'
    photos = UploadSet('photos', IMAGES)
    configure_uploads(app, (photos,))
    
    @main.route('/upload', methods=['GET', 'POST'])
    def upload():
        form = UploadForm()
        if form.validate_on_submit():
            if request.method == 'POST' and 'photo' in request.files:
                for img in request.files.getlist('photo'):
                    photos.save(img)
                flash("Photo saved.")
        return render_template('upload.html', form=form)
    

Form:

    
    
    from flask_wtf import Form
    from wtforms import SubmitField
    from flask_wtf.file import FileField, FileAllowed, FileRequired
    
    class UploadForm(Form):
        photo = FileField('Image', validators=[
        FileRequired(),
        FileAllowed(photos, 'Image only!')
    ])
        submit = SubmitField('Submit')
    

Template(use [Flask-Bootstrap](https://pythonhosted.org/Flask-
Bootstrap/forms.html)):

    
    
    {% import "bootstrap/wtf.html" as wtf %}
    <form class="form form-horizontal" method="POST" enctype="multipart/form-data">
        {{ form.hidden_tag() }}
        {{ wtf.form_field(form.photo, multiple="multiple") }}
        {{ wtf.form_field(form.submit) }}
    </form>
    

