<template>
<!-- 提交图片 -->
<view class="cu-card article">
<view class="cu-item shadow" style="background-color: transparent;">
<u-upload ref="uUpload" :action="action" :auto-upload="false" name="images" :size-type="sizeType"></u-upload>
<u-button @click="submit">提交</u-button>
</view>
</view>
</template>
class OrderSubmit(APIView):
def post(self, request):
# 订单图片对象
file_obj = request.FILES.getlist('images', None)
function = "submit_order"
# 上传图片 轮子写在下面
status, path = upload_files.UploadFilesOnce(request.user, function, file_obj)
if status == False:
message["code"] = 10041
message["message"] = "图片上传失败"
return JsonResponse(message)
def UploadFilesOnce(username,function,files):
try:
# 上传图片路径 media/username/task_id/images.jpg
dir_path = BASE_DIR + "/media/{}/{}/".format(function,username)
# 如果没有这个路径就创建
if not os.path.exists(dir_path):
os.makedirs(dir_path)
path=''
# 上传图片
for f in files:
# 为了避免覆盖 对名字进行加密
image_name = datamd5.md5(f.name) + '.jpg'
with open(dir_path + image_name, 'wb') as dest:
# 获取路径
path = path + '/media/{}/{}/'.format(function,username) + image_name + ","
for chunk in f.chunks():
dest.write(chunk)
return True,path
except:
# print(traceback.format_exc())
return False,None