【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
设备端上传日志
- 一次一个
- 一次多个文件
客户端要求:
serializers.py
class UploadSerializer(UploadBaseSerializer):
IMG_TYPE = ['gif', 'png', 'jpg', 'jpeg', 'webp']
LOG_TYPE = ['txt', 'log', 'zip', 'gz']
MP4_TYPE = ['mp4']
UPLOAD_TYPE = IMG_TYPE + LOG_TYPE + MP4_TYPE
file = serializers.ListField(
child=serializers.FileField(
required=True, help_text='上传文件',
validators=[
FileExtensionValidator(
UPLOAD_TYPE
)
]
)
)
def validate(self, attrs):
logger.debug("attrs:{}".format(attrs))
file_list = attrs.get('file')
if not file_list:
raise ValidationError(detail={
'file': _('file is None')
})
if len(file_list) == 1:
# 单文件
file_url = self.save_file(file_list[0])
attrs['data'] = file_url
return attrs
# 文件数组
url_list = []
for file in file_list:
file_url = self.save_file(file)
url_list.append(file_url)
attrs['data'] = url_list
return attrs
来源:oschina
链接:https://my.oschina.net/tplinuxhyh/blog/3145069