简单图片上传和文件保存的操作(没有用到数据库)
1、self.request.files 的使用和Python 操作文件,写入数据(open的使用)
- 首先写相应的上传文件的类
class UploadHandler(BaseHandler):
def get(self):
self.render('9upload.html')
def post(self):
try:
files = self.request.files['picture']#list类型中包含一个字典
if files[0]:
dict_img = files[0]
filename = dict_img['filename']
print(filename)
print(dict_img['content_type'])
with open('static/upload/{}'.format(filename), 'wb') as f:
f.write(dict_img['body'])
self.render('10upload_success.html', filename=files[0]['filename'])
else:
self.write("上传失败,系统不听话")
except:
self.write("上传失败,上传的图片不可以为空")
- 路由配置
handlers = [
(r'/upload', UploadHandler)
]
- 9upload.html
{% extends base.html %}
{% block title %}
上传图片界面
{% end %}
{% block body%}
<form action="/upload" enctype="multipart/form-data" method="post">
上传文件: <input type="file" name="picture"/><br>
<input type="submit"/>
</form>
{% end %}
- 10upload_success.html
{% extends base.html %}
{% block title %}
图片上传成功界面
{% end %}
{% block body%}
你成功上传了图片{{ filename }}!!!!,图片如下:<br>
<img src="{{ static_url('upload/{}'.format(filename)) }}" width="250" height="250">
{% end %}
2、用简单的目录检索来展示图片(使用 Python 标准库 glob)
- 首先我创建一个模块来生成图像路径列表upload_liat.py文件
from glob import glob
list_imgs = glob('static/upload/*')
new_list_img =[]
for list_img in list_imgs:
print(list_img)
new1 = list_img.replace('static/upload/', 'upload/')
new_list_img.append(new1)
- 然后在类中使用传到前端去进行渲染
from upload_liat import new_list_img
class UploadHandler(BaseHandler):
def get(self):
self.render('9upload.html')
def post(self):
try:
files = self.request.files['picture']#list类型中包含一个字典
if files[0]:
dict_img = files[0]
filename = dict_img['filename']
print(filename)
print(dict_img['content_type'])
with open('static/upload/{}'.format(filename), 'wb') as f:
f.write(dict_img['body'])
self.render('10upload_success.html', filename=files[0]['filename'], list_img=new_list_img)
else:
self.write("上传失败,系统不听话")
except:
self.write("上传失败,上传的图片不可以为空")
- 9upload.html和上面相同,10upload_success.html如下所示:
{% extends base.html %}
{% block title %}
图片上传成功界面
{% end %}
{% block body%}
你成功上传了图片{{ filename }}!!!!,图片如下:<br>
<img src="{{ static_url('upload/{}'.format(filename)) }}" width="250" height="250"><br>
{% set line="===="*6 %}
{{ line }}<br>
其他的上传图片还有如下几张:<br>
{% for img in list_img %}
<img src="{{ static_url(img) }}" width="250" height="250">
{% end %}
{% end %}
3、缩略图的生成
- 首先安装相应的库
pip install pillow
- pillow库的使用(相关类中进行编写)
from PIL import Image
class UploadHandler(BaseHandler):
def get(self):
self.render('9upload.html')
def post(self):
try:
files = self.request.files['picture']#list类型中包含一个字典
if files[0]:
dict_img = files[0]
filename = dict_img['filename']
print(filename)
print(dict_img['content_type'])
save_path = 'static/upload/{}'.format(filename)
with open(save_path, 'wb') as f:
f.write(dict_img['body'])
im = Image.open(save_path)
im.thumbnail((200, 200))
im.save('static/thumbs/thumb_{}'.format(filename))
self.render('10upload_success.html', filename=files[0]['filename'], list_img=new_list_img, new_list_thumb=new_list_thumb)
else:
self.write("上传失败,系统不听话")
except Exception as e:
print(e)
self.write("上传失败,上传的图片不可以为空")
- 例外的简单实例
from PIL import Image
import glob, os
imgs = glob.glob('*.jpg')
for img in imgs:
name, ext = os.path.splitext(img)
im = Image.open(img)
im.thumbnail((200, 200))
im.save("{}_{}x{}.jpg".format(name, 200, 200), "JPEG")
网友评论