0

I have tried to upload folder which contains various folder with different files in it. I have tried to do with flask and which I couldn't find. please help me out to upload folders in specific directory

Please help me out to upload folders in specific directory with flask and jinja

from flask import Flask, render_template,redirect,request,url_for
from flask_wtf import FlaskForm
from wtforms import FileField, SubmitField
from werkzeug.utils import secure_filename
import os

app = Flask(__name__)
app.config['SECRET_KEY'] = 'supersecretkey'
app.config['UPLOAD_FOLDER'] = 'static/files'

class UploadFileForm(FlaskForm):
    file = FileField("File", validators=[InputRequired()])
    submit = SubmitField("Upload File")

@app.route('/', methods=['GET',"POST"])
@app.route('/home', methods=['GET',"POST"])
def home():
    form = UploadFileForm()
    if form.validate_on_submit():
        file = form.file.data # First grab the file
        file.save(os.path.join(os.path.abspath(os.path.dirname(__file__)),app.config['UPLOAD_FOLDER'],secure_filename(file.filename))) # Then save the file
        return "File has been uploaded."
    return render_template('index.html', form=form)

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

HTML page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home</title>
</head>
<body>
    <h1>Flask File Uploads Tutorial</h1>

    <form method='POST' enctype='multipart/form-data'>
        {{form.hidden_tag()}}
        {{form.file()}}
        {{form.submit()}}
    </form>
</body>
</html>
rohin
  • 31
  • 2
  • Does this answer your question? [How to upload multiple files with flask-wtf?](https://stackoverflow.com/questions/53890136/how-to-upload-multiple-files-with-flask-wtf) – pjcunningham Nov 17 '22 at 09:36

1 Answers1

0

You can add render_kw parameter with the dictionary {'webkitdirectory': True}.

Example:

img_dataset = FileField('img_dataset', validators=[FileRequired(),
    FileAllowed(images, 'Please select image dataset')], render_kw={'webkitdirectory': True})

render_kw (dict)

– If provided, a dictionary which provides default keywords that will be given to the widget at render time.

ref

This attribute support plateform: enter image description here ref1

Creek
  • 207
  • 2
  • 12