python相关

文件上传

2019-01-07  本文已影响0人  十万个榴莲饼
"""文件上传"""
class FileUploadView(APIView):
    '''
    上传文件接口
    '''
    permission_classes = (IsAuthenticated,) # 通过认证的用户上传
    parser_classes = (MultiPartParser,FileUploadParser)
    def post(self,request,format=None):
        """
            返回上传的图片地址
        """
        try:
            files = request.FILES.getlist('file', None)  # 图片
            filemkdir = request.POST.get('filemkdir', None)  # 目录
            if filemkdir not in settings.DATA_FILENAAME or not files:
                return Response({"code":400, "msg":u"上传参数无效"})
            if filemkdir == 'attachment':
                self.IMG_result = self.attachment_uploading(files)
            else:
                self.IMG_result = self.file_upload(files=files,mk=filemkdir)
            return Response(self.IMG_result)
        except Exception as e:
            print(e)
            return Response({"code": 400, "msg": u"上传失败"})
上一篇下一篇

猜你喜欢

热点阅读