文件上传

"""文件上传"""
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"上传失败"})

你可能感兴趣的:(文件上传)