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()
pip install -e "git://github.com/FelixLoether/flask-uploads#egg=Flask-Uploads"
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.
Auto-incrementing integer field. Primary key.
Unicode string field of length 255. The name of the original upload.
Unicode string field of length 255. Absolute URL to the original upload.
Unicode string field of length 255. The name of the image resized to {size}. None if the upload was not an image file.
Unicode string field of length 255. Absolute URL to the image resized to {size}. None if the upload was not an image file.
Initializes the extension.
Parameters: |
|
---|
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: |
|
---|
Deletes the uploaded file.
Parameters: | upload (Upload) – The upload to remove. |
---|
Saves data as a new upload with name name. Used by save().
Parameters: |
|
---|
Saves data as a new upload with the given images. Used by save().
Parameters: |
|
---|
Full-text doc search.