Skip to content

Instantly share code, notes, and snippets.

@greyli
Last active February 25, 2024 03:04
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save greyli/ca74d71f13c52d089a8da8d2b758d519 to your computer and use it in GitHub Desktop.
Save greyli/ca74d71f13c52d089a8da8d2b758d519 to your computer and use it in GitHub Desktop.
Photo upload and manage with Flask and Flask-Uploads (Multiple file upload support!).
# -*- coding: utf-8 -*-
import os
import uuid
from flask import Flask, render_template, redirect, url_for, request
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileRequired, FileAllowed
from wtforms import SubmitField
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SECRET_KEY'] = 'I have a dream'
app.config['UPLOADED_PHOTOS_DEST'] = os.path.join(basedir, 'uploads') # you'll need to create a folder named uploads
photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)
patch_request_class(app) # set maximum file size, default is 16MB
class UploadForm(FlaskForm):
photo = FileField(validators=[FileAllowed(photos, 'Image Only!'), FileRequired('Choose a file!')])
submit = SubmitField('Upload')
@app.route('/', methods=['GET', 'POST'])
def upload_file():
form = UploadForm()
if form.validate_on_submit():
for f in request.files.getlist('photo'):
filename = uuid.uuid4().hex
photos.save(f, name=filename + '.')
success = True
else:
success = False
return render_template('index.html', form=form, success=success)
@app.route('/manage')
def manage_file():
files_list = os.listdir(app.config['UPLOADED_PHOTOS_DEST'])
return render_template('manage.html', files_list=files_list)
@app.route('/open/<filename>')
def open_file(filename):
file_url = photos.url(filename)
return render_template('browser.html', file_url=file_url)
@app.route('/delete/<filename>')
def delete_file(filename):
file_path = photos.path(filename)
os.remove(file_path)
return redirect(url_for('manage_file'))
if __name__ == '__main__':
app.run(debug=True)
<!DOCTYPE html>
<title>File Browser</title>
<h1>File Browser</h1>
<a href="{{ url_for('upload_file') }}">Upload</a> /
<a href="{{ url_for('manage_file') }}">Manage</a>
<hr>
<p>URL: {{ file_url }}</p>
<img src="{{ file_url }}">
<!DOCTYPE html>
<title>Upload File</title>
<h1>Upload File</h1>
<a href="{{ url_for('upload_file') }}">Upload</a> /
<a href="{{ url_for('manage_file') }}">Manage</a>
<hr>
<form method="POST" enctype="multipart/form-data">
{{ form.hidden_tag() }}
{{ form.photo(multiple="multiple")}}
{% for error in form.photo.errors %}
<span style="color: red;">{{ error }}</span>
{% endfor %}
{{ form.submit }}
</form>
{% if success %}
<br>
<p>Upload Success!</p>
{% endif %}
<!DOCTYPE html>
<title>File Manager</title>
<h1>File Manager</h1>
<a href="{{ url_for('upload_file') }}">Upload</a> /
<a href="{{ url_for('manage_file') }}">Manage</a>
<hr>
{% for photo in files_list %}
- {{ photo }}
<a href="{{ url_for('open_file', filename=photo) }}">open</a>
<a href="{{ url_for('delete_file', filename=photo) }}">del</a><br>
{% endfor %}
@jevy146
Copy link

jevy146 commented Apr 20, 2020

ImportError: cannot import name 'secure_filename'

@greyli
Copy link
Author

greyli commented Apr 20, 2020

ImportError: cannot import name 'secure_filename'

Please post the full error traceback.

@jevy146
Copy link

jevy146 commented Apr 20, 2020

  name = hashlib.md5('admin' + str(time.time())).hexdigest()[:15]

TypeError: Unicode-objects must be encoded before hashing

This will run normally

@app.route('/', methods=['GET', 'POST'])
def upload_file():
form = UploadForm()
if form.validate_on_submit():
for filename in request.files.getlist('photo'):
str_name='admin' + str(int(time.time()))
name = hashlib.md5(str_name.encode("utf-8")).hexdigest()[:15]
photos.save(filename, name=name + '.')
success = True
else:
success = False
return render_template('index.html', form=form, success=success)

@rishabhm74
Copy link

Reason is incompatible with newly released Werkzeug . You need to install werkzeug==0.16.1 and also connexion. Hope this helps!

@richardfogaca
Copy link

Very useful, thanks!
For those who are struggling with 'ImportError: cannot import name 'secure_filename', just install Flask-Reuploaded and the error message will be gone.
https://pypi.org/project/Flask-Reuploaded/

@tassaron
Copy link

Since the hashed filename isn't used for any purpose other than uniqueness, you could replace it with something like str(uuid.uuid4()) and achieve the same goal

@greyli
Copy link
Author

greyli commented Dec 16, 2020

@tassaron2 Updated, thanks for the tip.

@vio1etus
Copy link

vio1etus commented Apr 13, 2021

Reason is incompatible with newly released Werkzeug . You need to install werkzeug==0.16.1 and also connexion. Hope this helps!

Help me a lot,thanks!

@vio1etus
Copy link

vio1etus commented Apr 13, 2021

Very useful, thanks!
For those who are struggling with 'ImportError: cannot import name 'secure_filename', just install Flask-Reuploaded and the error message will be gone.
https://pypi.org/project/Flask-Reuploaded/

It will introduce other errors to this project from what I tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment