Python 3 Flask Upload Multiple Files or Images Using HTML5 Form in Browser Full Project For Beginners

 

 

app.py

 

 

from flask import Flask, render_template, request
#from werkzeug import secure_filename
from werkzeug.utils import secure_filename
app = Flask(__name__)

@app.route('/')
def upload_file():
   return render_template('upload.html')

@app.route('/uploader', methods = ['GET', 'POST'])
def upload_file1():
   if request.method == 'POST':
      files = request.files.getlist("file")
      for file in files:
          file.save(secure_filename(file.filename))
      return 'file uploaded successfully'

if __name__ == '__main__':
   app.run(debug = True)

 

 

Now make the templates folder and inside it make a upload.html file and copy paste the following code

 

 

upload.html

 

 

<html>
   <body>
      <form action = "http://localhost:5000/uploader" method = "POST" 
         enctype = "multipart/form-data">
         <input type = "file" name = "file" multiple/>
         <input type = "submit"/>
      </form>
   </body>
</html>

 

 

Now run the flask app by running the below command

 

 

python app.py

 

 

 

 

 

Leave a Reply