Skip to main content

Library that works with Flask & SqlAlchemy to store files in your database and server.

Project description

Documentation Status PyPI version PyPI - Python Version PyPI - License


Library that works with Flask & SqlAlchemy to store files on your server & in your database

Read the docs: Documentation


pip install flask-file-upload

Flask File Upload

General Flask config options
    # Important: The below configuration variables need to be set  before
    # initiating `FileUpload`
    UPLOAD_FOLDER = join(dirname(realpath(__file__)), "uploads/media")
    ALLOWED_EXTENSIONS = ["jpg", "png", "mov", "mp4", "mpg"]
    MAX_CONTENT_LENGTH` = 1000 * 1024 * 1024  # 1000mb
    SQLALCHEMY_DATABASE_URI = "postgresql://localhost:5432/blog_db"

We can either pass the instance to FileUpload(app) or to the init_app(app) method:

app = Flask(__name__)

db = SQLAlchemy()
# Important! See documentation for set up specifics
file_upload = FileUpload(app, db)

# An example using the Flask factory pattern
def create_app():

# Or we can pass the Flask app instance directly & the Flask-SQLAlchemy instance:
db = SQLAlchemy(app)
file_upload = FileUpload(app, db)
app: Flask = None
Decorate your SqlAlchemy models

Flask-File-Upload (FFU) setup requires each SqlAlchemy model that wants to use FFU library to be decorated with @file_upload.Model .This will enable FFU to update your database with the extra columns required to store files in your database. Declare your attributes as normal but assign a value of file_upload.Column & pass the SqlAlchemy db instance: file_upload.Column(db). This is easy if you are using Flask-SqlAlchemy:

from flask_sqlalchemy import SqlAlchemy

db = SqlAlchemy()

Full example:

from my_app import db, file_upload

class blogModel(db.Model):
   __tablename__ = "blogs"
   id = db.Column(db.Integer, primary_key=True)

   # Your files -  Notice how we pass in the SqlAlchemy instance
   # or `db` to the `file_uploads.Column` class:

   my_placeholder = file_upload.Column(db)
   my_video = file_upload.Column(db)
define files to be upload:
(This is an example of a video with placeholder image attached):
    my_video = request.files["my_video"]
    placeholder_img = request.files["placeholder_img"]
Save files
    file_upload.save_files(blog_post, files={
        "my_video": my_video,
        "placeholder_img": placeholder_img,
Update files
    blog_post = file_upload.update_files(blog_post, files={
        "my_video": new_my_video,
        "placeholder_img": new_placeholder_img,
Delete files
    file_upload.delete_files(BlogPostModel, files=["my_video"])
Stream a file
    file_upload.stream_file(blog_post, filename="my_video")
File Url paths
    file_upload.get_file_url(blog_post, filename="placeholder_img")

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for flask-file-upload, version 0.0.11
Filename, size File type Python version Upload date Hashes
Filename, size flask-file-upload-0.0.11.tar.gz (10.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page