FileUploadParser doesn't get the file name

In django REST framework. we have components like Parsers, Renderers and Serializers.

  • The responsibility of Parsers is to parse the data that is sent by request methods GET, POST and PUT, etc.

  • Default parser used in django REST is 'JSONParser'. It only parses the data JSON data[numbers, string, date]. It ignores the data like FILES.

  • In order to parse the FILES we need to use parsers like "MultiPartParser" or "FormParser".

    Example Code :

        from rest_framework.parsers import MultiPartParser
        from rest_framework.response import Response
        from rest_framework.views import APIView
    
        class ExampleView(APIView):
            """
            A view that can accept POST requests with JSON content.
            """
            parser_classes = (MultiPartParser,)
    
            def post(self, request, format=None):
                # to access files
                print request.FILES
                # to access data
                print request.data
                return Response({'received data': request.data})
    

When we use property request.data then parser will parse the data.

References: Django REST Docs, Django REST Github


I face the same problem.The problem error message shows:

{"detail":"Missing filename. Request should include a Content-Disposition header with a filename parameter."} I do all the steps above my answer but it doesn't work.Finally,

I find the reason is in the backend of viewset.

it shows like this

parser_classes = (FileUploadParser, MultiPartParser, FormParser)

and I remove the FileUploadParser

  parser_classes = ( MultiPartParser, FormParser)

and It works, so I think you should pay more attention to it