[Django]-FileUploadParser doesn't get the file name

37๐Ÿ‘

โœ…

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

4๐Ÿ‘

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

๐Ÿ‘คDeft-pawN

3๐Ÿ‘

Adding

Content-Disposition: attachment; filename=<insert-your-file-name>

to the header has solved my issue, and then in the view method where you access this file in the following manner:

file = self.request.FILES['file']
๐Ÿ‘คAli H. Kudeir

2๐Ÿ‘

On the second error Missing filename. Request should include a Content-Disposition header with a filename parameter. using postman I removed the header Content-Type :multipart/form-data and was successful.

It seems the issue is the custom Content-Type header overrides the default Content-Type header that should be sent. Check this thread for reference Content-Type for multipart posts

๐Ÿ‘คStackEdd

2๐Ÿ‘

You donโ€™t need to use MultipartParser or FormParser.

What you need is a serializer with a FileField() like so:

serializers.py:

class FileUploadSerializer(serializers.Serializer):
    # I set use_url to False so I don't need to pass file 
    # through the url itself - defaults to True if you need it
    file = serializers.FileField(use_url=False)

So then when you try to access file here below, youโ€™ll have a dict with a key named file. Personally, Iโ€™d probably name it something more descriptive than just โ€œfileโ€, but thatโ€™s up to you.

from .serializers import FileUploadSerializer

    class FileUploadView(APIView):

       def post(self, request):
           # set 'data' so that you can use 'is_vaid()' and raise exception
           # if the file fails validation
           serializer = FileUploadSerializer(data=request.data)
           serializer.is_valid(raise_exception=True)
           # once validated, grab the file from the request itself
           file = request.FILES['file']
๐Ÿ‘คPaul Jurczyk

0๐Ÿ‘

in your views.py change the parser like this

parser_classes = (JSONParser, MultiPartParser)

๐Ÿ‘คDarwin

Leave a comment