1 回答
TA贡献1780条经验 获得超1个赞
<form method="post" action="{{ url_for('') }}" enctype="multipart/form-data">
def upload_image():
try:
if request.method == 'POST':
ALLOWED_EXTENSIONS = [".png", ".jpg", ".jpeg", ".gif"]
file = request.files['image']
if file and any(split_filename(file.filename)[1] == s for s in ALLOWED_EXTENSIONS):
folder = app.config['UPLOAD_FOLDER']
pathName = app.config['IMAGE_PATH'] + datetime.utcnow().strftime(
'%Y\\%m\\')
if not os.path.exists(os.path.join(folder + pathName)):
os.makedirs(folder + pathName)
filename = str(uuid.uuid4()) + split_filename(file.filename)[1]
file.save(os.path.join(folder + pathName, filename))
path = pathlib.PureWindowsPath(pathName + filename).as_posix()
return url_for('main.get_file', path=path, _external=True)
else:
return 'Please Choose PNG, JPG, JPEG, GIF Image, Not ' + split_filename(file.filename)[1], 404
except Exception as error:
return error.__str__()
@mn.route('/file/<path:path>', methods=['GET'])
def get_file(path):
try:
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], path))
except :
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], '404.png'))
这是我的代码
- 1 回答
- 0 关注
- 65 浏览
添加回答
举报