1

Hello,
I am trying to handle errors when uploading a file with a disallowed filetype or when submiting without selecting any file.
All I get is "flask_uploads.exceptions.UploadNotAllowed" and on the web page "Internal Server Error". I want to flash an error message but have no idea how to handle the error. I googled and I read the documentation but I couldn't find a way.
Everything works fine when selecting and submiting the right type of file/files(in this case IMAGES).
Thank you!

if request.method == 'POST':
        if form.validate_on_submit() and 'artwork' in request.files:

            art = form.artwork.data


            this_artf = art_f+'/'+str(order_no_art)

            app.config['UPLOADED_IMAGES_DEST'] = this_artf
            images = UploadSet('images', IMAGES)
            configure_uploads(app, images)

            for image in art:
                images.save(image)
Endriu Andrei
  • 53
  • 1
  • 5

2 Answers2

2

As you said, you have looked in the documentation...

I just enhanced the example shown at https://github.com/jugmac00/flask-reuploaded with handling the mentioned UploadNotAllowed exception.

Be sure to not forget to import the exception first!

...
from flask_uploads.exceptions import UploadNotAllowed
...

@app.route("/", methods=['GET', 'POST'])
def upload():
    if request.method == 'POST' and 'photo' in request.files:
        try:
            photos.save(request.files['photo'])
            flash("Photo saved successfully.")
            return render_template('upload.html')
        except UploadNotAllowed:
            flash("File type not allowed!")
            return render_template('upload.html')
    return render_template('upload.html')

This is a generic answer, but I am positive you can apply it to your case.

By the way, I saw you configured the app within the request handling:

if request.method == 'POST':
    if form.validate_on_submit() and 'artwork' in request.files:
        art = form.artwork.data
        this_artf = art_f+'/'+str(order_no_art)
        app.config['UPLOADED_IMAGES_DEST'] = this_artf
        images = UploadSet('images', IMAGES)
        configure_uploads(app, images)

While this currently works, this is not the way it is supposed to do.

It is about these lines...

app.config['UPLOADED_IMAGES_DEST'] = this_artf
images = UploadSet('images', IMAGES)
configure_uploads(app, images)

You have to move those lines outside the request context, e.g. at the top of your module or in your application factory.

Disclaimer: I am the maintainer of Flask-Reuploaded.

Jürgen Gmach
  • 5,366
  • 3
  • 20
  • 37
  • Wow thank you so much for taking the time to answer my beginner question! About configuring the app in the request handling, I am doing that because the folder where the picture needs to be saved it is only created then for that order when the art is uploaded. – Endriu Andrei May 07 '21 at 14:35
1

Handle flask_uploads.exceptions.UploadNotAllowed exception when invoking save method on instance of UploadSet.

from flask_uploads.exceptions import UploadNotAllowed
from flask import flash # make sure to configure secret key for your app
#....

error_files = []
for image in art:
    try:
        images.save(image)
    except flask_uploads.exceptions.UploadNotAllowed as err:
        error_files.append(image.filename)
        continue

flash(error_files, 'error')

You could then, present the files in the rendered template by getting the flashed messages.

Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81