![]() ![]() ![]() When Flask receives a file submission it does not automatically write it to disk. If the user submits the form without selecting a file in the file field, then the filename is going to be an empty string, so it is important to always check the filename to determine if a file is available or not. The filename attribute in the FileStorage provides the filename submitted by the client. This is an instance of class FileStorage, which Flask imports from Werkzeug. The uploaded_file variable holds the submitted file object. Note how the same root URL is split between two view functions, with index() set to accept the GET requests and upload_file() the POST ones. ![]() The upload_file() function is decorated with so that it is invoked when the browser sends a POST request. Uploaded_file.save(uploaded_file.filename) Ignoring important aspects such as validation and security for the moment, the short Flask application shown below accepts a file uploaded with the form shown in the previous section, and writes the submitted file to the current directory: from flask import Flask, render_template, request, redirect, url_for This also happens with file fields that allow multiple files. This is necessary because forms can include multiple fields with the same name, as is often the case with groups of check boxes. ![]() The request.form and request.files dictionaries are really "multi-dicts", a specialized dictionary implementation that supports duplicate keys. File fields, however, are included in the request.files dictionary. Examples:įor regular forms, Flask provides access to submitted form fields in the request.form dictionary.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |