django rest framework uploadFile并保存在服务器

  • 时间:
  • 来源:互联网
  • 文章标签:
from rest_framework.parsers import MultiPartParser
from rest_framework.decorators import parser_classes

from django.views.decorators.csrf import csrf_exempt#不进行csrf验证

from rest_framework.authentication import SessionAuthentication, TokenAuthentication
from rest_framework.permissions import IsAuthenticated


@api_view(['POST'])
@csrf_exempt
@parser_classes((MultiPartParser,))#参数类型
@authentication_classes((TokenAuthentication, SessionAuthentication))
 @permission_classes((IsAuthenticated,))
def upload_file(request):
    file_obj = request.FILES.get('file', None)
    filename = 'path/aaa.xls'
    destination = open(filename, 'wb+')
    for chunk in file_obj.chunks():
        destination.write(chunk)
    destination.close()
    return Response({'code': '200', 'message': 'OK'})

本文链接http://www.taodudu.cc/news/show-647563.html