4

I need to create a utility which can store an entire folder, folders or files through Flask, at the moment I am using the following code to upload 1 file at a time

flask code:

from flask import Flask, render_template, request

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':
      f = request.files['file']
      f.save(secure_filename(f.filename))
      return 'file uploaded successfully'

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

html code:

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

and I receive the following dialog box:

dialog box

but this only allows me to insert a single file at a time.

I require something which can allow me to upload an entire directory or multiple files together. Can it be done using Flask? If not, is there any alternative which is available in Python? Any help on the matter will really be appreciated!

Saurav Saha
  • 745
  • 1
  • 11
  • 30
  • 1
    Does this answer your question? [Uploading multiple files with Flask](https://stackoverflow.com/questions/11817182/uploading-multiple-files-with-flask) – metatoaster Feb 11 '20 at 11:27
  • Nope, I have already gone through that thread, but nothing worked for me. – Saurav Saha Feb 11 '20 at 11:36
  • 2
    Have you carefully studied and compared the difference in the code you have provided and the code in the linked thread, for example the inclusion of the [`multiple` attribute in the `` tag which is needed to permit selection of multiple files](https://stackoverflow.com/questions/1593225/how-to-select-multiple-files-with-input-type-file) in the HTML? Have you noticed that you may need to investigate the usage of `getlist`? – metatoaster Feb 11 '20 at 11:47
  • oh! so that's what I was missing. Sorry! It was very careless of me :( – Saurav Saha Feb 13 '20 at 09:08

1 Answers1

5

answer:

html file

<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>

in the input tag, multiple is a mandatory argument to allow access of multiple files!

flask code:

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)

Use flask.request.getlist to get the list of files in the directory. To process multiple files just use a loop to manage them as shown above.

Saurav Saha
  • 745
  • 1
  • 11
  • 30
  • On the client side, I added webkitdirectory mozdirectory to the form input as follows and was able to upload the whole folder including nested files. The server code is more or less the same (the difference is only that of my own logic.) I tested it in Firefox: – Toàn Nguyễn Apr 28 '21 at 06:06