Navigation

Flask-Upload¶

Flask-Upload is a Flask extension to help you add file uploading functionality to your site. Here’s a small example on utilizing Flask-Upload:

import os.path
from flask import Flask, redirect, request, render_template, url_for
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.storage import get_default_storage_class
from flask.ext.uploads import delete, init, save, Upload

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
app.config['DEFAULT_FILE_STORAGE'] = 'filesystem'
app.config['UPLOADS_FOLDER'] = os.path.realpath('.') + '/static/'
app.config['FILE_SYSTEM_STORAGE_FILE_VIEW'] = 'static'
init(SQLAlchemy(app), get_default_storage_class(app))


@app.route('/')
def index():
    """List the uploads."""
    uploads = Upload.query.all()
    return render_template('list.html', uploads=uploads)


@app.route('/upload', methods=['GET', 'POST'])
def upload():
    """Upload a new file."""
    if request.method == 'POST':
        save(request.files['upload'])
        return redirect(url_for('index'))
    return render_template('upload.html')


@app.route('/delete/<int:id>', methods=['POST'])
def remove(id):
    """Delete an uploaded file."""
    upload = Upload.query.get_or_404(id)
    delete(upload)
    return redirect(url_for('index'))

if __name__ == '__main__':
    app.run()

Installation¶

pip install -e "git://github.com/FelixLoether/flask-uploads#egg=Flask-Uploads"

API Reference¶

class flask.ext.uploads.Upload

The database model class generated based on some preset fields and the resizer argument passed to init(). Each of the resizer’s sizes add a {size}_name and a {size}_url field to the model.

id

Auto-incrementing integer field. Primary key.

name

Unicode string field of length 255. The name of the original upload.

url

Unicode string field of length 255. Absolute URL to the original upload.

{size}_name

Unicode string field of length 255. The name of the image resized to {size}. None if the upload was not an image file.

{size}_url

Unicode string field of length 255. Absolute URL to the image resized to {size}. None if the upload was not an image file.

flask.ext.uploads.init(db, Storage, resizer=None)

Initializes the extension.

Parameters:
  • db (Flask-SQLAlchemy object) – Used for saving the file data.
  • Storage (Flask-Storage storage class) – The class whose object’s are used for saving the files.
  • resizer (Resizer) – Used for image resizing. If not present, images are not resized.
flask.ext.uploads.save(data, name=None)

Saves data to a new file. If data is an image and resizer was provided for init(), the image will be resized to all of the resizer’s sizes.

Parameters:
  • data (file-like object) – The data to save. Must have a read() method and, if name was not provided, a filename attribute.
  • name (unicode) – The name to use when saving the data. Defaults to data.filename.
flask.ext.uploads.delete(upload)

Deletes the uploaded file.

Parameters:upload (Upload) – The upload to remove.
flask.ext.uploads.save_file(name, data)

Saves data as a new upload with name name. Used by save().

Parameters:
  • name (unicode) – The name to use when saving the upload.
  • data (file-like object or unicode) – The original upload data.
flask.ext.uploads.save_images(name, data, images)

Saves data as a new upload with the given images. Used by save().

Parameters:
  • name (unicode) – The name to use when saving the upload.
  • data (file-like object or unicode) – The original upload data.
  • images (dict) – A dictionary containing the names and datas of the images, as returned by Resizer.resize_image.

Project Versions

Table Of Contents

  • Flask-Upload
    • Installation
    • API Reference

This Page

  • Show Source

Navigation

© Copyright 2012, Oskari Hiltunen. Created using Sphinx 1.1.3.
TEST Brought to you by Read the Docs
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.