0

I'm using the flask-user extension (https://flask-user.readthedocs.io/) along with the mongoengine adapter and flask-wtf to create an HTML form that'll accept several files (images, mp4). The goal is to directly store those files in MongoDB using GridFS but I can't get it to work without storing the image locally on the flask server first...

Here's my non-working code:

@bp.route('/video1', methods=['GET', 'POST'])
@login_required
def video1():
    video1Form = Video1Form()
    if request.method == 'POST' and video1Form.validate_on_submit():
        newVideo1 = Video1(
            slogan = video1Form.slogan.data,
            objectif = video1Form.objectif.data,
            stat1 = video1Form.stat1.data,
            stat2 = video1Form.stat2.data
        )
        name = secure_filename(str(current_user.id) + "__" + "logo.png")
        fh = open(video1Form.logo.data, 'rb')
        newVideo1.logo.put(fh, filename=name, content_type = "image/jpeg")
        newVideo1.save()
        return redirect(url_for('video.download'))
    if video1Form.errors:
        flash(video1Form.errors, 'alert-danger')
    return render_template('video/video1.html', video1Form=video1Form)
`

I get the following error on:

[...]File "/home/lucaspierru/Documents/IKADA/sos_sponsors/app/video/routes.py", line 35, in video1 fh = open(video1Form.logo.data, 'rb')

FileNotFoundError: [Errno 2] No such file or directory: 'name_of_the_uploaded_file.ext'

Is it possible to point directly to the file's content and not just get the filename without saving it to the server first ?

Lucas P.
  • 125
  • 3
  • 12

1 Answers1

0

I found my mistake:

I forgot to add the enctype="multipart/form-data" attribute to my HTML <form> tag and apparently it is needed to properly upload files, otherwise they'll be considered as text data.

Lucas P.
  • 125
  • 3
  • 12