Skip to content

Instantly share code, notes, and snippets.

@rodrigobertin
Created August 22, 2019 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigobertin/e7d91726845e5dcb65c2053e5443bf61 to your computer and use it in GitHub Desktop.
Save rodrigobertin/e7d91726845e5dcb65c2053e5443bf61 to your computer and use it in GitHub Desktop.
Upload multiple images
def upload_images(upload_folder, input_name='image'):
"""
Upload images with flask
:param multiple:
:param upload_folder:
:param input_name:
:return:
"""
from werkzeug.utils import secure_filename
import os
import random
from flask import request
allowed_types = ['image/jpg', 'image/jpeg', 'image/png', 'image/svg+xml', 'image/gif']
if not upload_folder:
msg = u'No se especifico carpeta de subida'
print(msg)
raise Exception(msg)
if input_name in request.files:
file_uploaded = request.files.getlist(input_name)
final_list = []
for img in file_uploaded:
if img.filename == '':
msg = u'Error de nombre de archivo'
print(msg)
raise Exception(msg)
if img.content_type in allowed_types:
filename = u'{}_{}'.format(random.randint(999, 9999), secure_filename(img.filename))
upload = img.save(os.path.join(upload_folder, filename))
print(u'---------> upload: {}'.format(upload))
final_list.append(filename)
else:
msg = u'Archivo {} no permitido'.format(img.filename)
print(msg)
raise Exception(msg)
return final_list
else:
msg = u'Sin archivo'
print(msg)
raise Exception(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment