

form = ProfileForm(CombinedMultiDict((request.files, request.form))) You can use a CombinedMultiDict to combine the two into a single structure that WTForms understands. However, since Flask splits the form data ( request.form) and the file data ( request.files), you need to make sure to pass the correct data when creating the form.

It doesn't do anything special with the uploaded data. WTForms provides a FileField to render a file type input. Profile.save(os.path.join(uploads_dir, secure_filename(profile.filename)))įor file in ('charts'):įile.save(os.path.join(uploads_dir, secure_filename(file.name))) Uploads_dir = os.path.join(app.instance_path, 'uploads') # Create a directory in a known location to save files to. import osįrom flask import render_template, request, redirect, url_for This doesn't ensure that the name is unique, so existing files will be overwritten unless you do extra work to detect that. This can be set arbitrarily by the client, so pass it through the secure_filename method to generate a valid and safe name to save as.

The filename attribute is the name the file was uploaded with. Create a common directory to save the files to. The objects in request.files have a save method which saves the file locally. ('charts') # list of files (even if one was sent) request.files # single file (even if multiple were sent) Use getlist - instead of or get - if multiple files were uploaded with the same field name. Uploaded files are available in request.files, a MultiDict mapping field names to file objects. For examples using your favorite tool or more complex scenarios, see the docs for that tool. This is not meant to be an exhaustive list. This demonstrates a list of (name, data) tuples, which allows multiple files like the form above.

Requests is a powerful Python library for making HTTP requests.
