python上传图片到本地

from face_recognition(我的主应用文件夹) import settings


fp = request.FILES.get("file")
if not fp:
return msgJson.jsonMsg('上传样本为空', 0)
# 文件名
name = str(mobile) + datetime.now().strftime("%Y%m%d%H%M%S")
# fp 获取到的上传文件对象
path = os.path.join(settings.STATICFILES_DIRS[1], "%s.jpg" % name) # 上传文件本地保存路径, settings.STATICFILES_DIRS[1]文件夹专门存放图片的文件夹(在设置里面有个STATICFILES_DIRS静态文件夹数组)
if fp.multiple_chunks(): # 判断上传文件大于2.5MB的大文件
# 为真
file_yield = fp.chunks() # 迭代写入文件
with open(path, 'wb') as f:
for buf in file_yield: # for情况执行无误才执行 else
f.write(buf)
else:
with open(path, 'wb') as f:
f.write(fp.read())
posted @ 2020-12-14 17:28  Smile☆  阅读(734)  评论(0编辑  收藏  举报