How to return generated file download with Django REST Framework?

41,175

Solution 1

I solved my problem by saving file in media folder and sending of the link of it to front-end.

@permission_classes((permissions.IsAdminUser,))
class StudentDocxViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    def retrieve(self, request, *args, **kwargs):
        template = webodt.ODFTemplate('test.odt')
        queryset = Pupils.objects.get(id=kwargs['pk'])
        serializer = StudentSerializer(queryset)
        context = dict(serializer.data)
        document = template.render(Context(context))
        doc = converter().convert(document, format='doc')
        p = u'docs/cards/%s/%s_%s.doc' % (datetime.now().date(), context[u'surname'], context[u'name'])
        path = default_storage.save(p, doc)
        return response.Response(u'/media/' + path)

And handled this like in my front-end (AngularJS SPA)

$http(req).success(function (url) {
    console.log(url);
    window.location = url;
})

Solution 2

Here's an example of returning a file download directly from DRF. The trick is to use a custom renderer so you can return a Response directly from the view:

from django.http import FileResponse
from rest_framework import viewsets, renderers
from rest_framework.decorators import action

class PassthroughRenderer(renderers.BaseRenderer):
    """
        Return data as-is. View should supply a Response.
    """
    media_type = ''
    format = ''
    def render(self, data, accepted_media_type=None, renderer_context=None):
        return data

class ExampleViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Example.objects.all()

    @action(methods=['get'], detail=True, renderer_classes=(PassthroughRenderer,))
    def download(self, *args, **kwargs):
        instance = self.get_object()

        # get an open file handle (I'm just using a file attached to the model for this example):
        file_handle = instance.file.open()

        # send file
        response = FileResponse(file_handle, content_type='whatever')
        response['Content-Length'] = instance.file.size
        response['Content-Disposition'] = 'attachment; filename="%s"' % instance.file.name

        return response

Note I'm using a custom endpoint download instead of the default endpoint retrieve, because that makes it easy to override the renderer just for this endpoint instead of for the whole viewset -- and it tends to make sense for list and detail to return regular JSON anyway. If you wanted to selectively return a file download you could add more logic to the custom renderer.

Solution 3

This may work for you:

file_path = file_url
FilePointer = open(file_path,"r")
response = HttpResponse(FilePointer,content_type='application/msword')
response['Content-Disposition'] = 'attachment; filename=NameOfFile'

return response.

For FrontEnd code refer this

Solution 4

I am using DRF and i found a view code to download file, which would be like

from rest_framework import generics
from django.http import HttpResponse
from wsgiref.util import FileWrapper

class FileDownloadListAPIView(generics.ListAPIView):

    def get(self, request, id, format=None):
        queryset = Example.objects.get(id=id)
        file_handle = queryset.file.path
        document = open(file_handle, 'rb')
        response = HttpResponse(FileWrapper(document), content_type='application/msword')
        response['Content-Disposition'] = 'attachment; filename="%s"' % queryset.file.name
        return response

and url.py will be

path('download/<int:id>/',FileDownloadListAPIView.as_view())

I am using React.js in frontend and i get a response like

handleDownload(id, filename) {
  fetch(`http://127.0.0.1:8000/example/download/${id}/`).then(
    response => {
      response.blob().then(blob => {
      let url = window.URL.createObjectURL(blob);
      let a = document.createElement("a");
      console.log(url);
      a.href = url;
      a.download = filename;
      a.click();
    });
  });
}

and after i got successful in downloading a file which also opens correctly and i hope this gonna work. Thanks

Solution 5

For me, using Python 3.6, Django 3.0, and DRF 3.10, The problem came from using the wrong type of response. I needed to use a django.http.HttpResponse, as seen below:

from django.http import HttpResponse
...
with open('file.csv', 'r') as file:
    response = HttpResponse(file, content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=file.csv'
    return response
Share:
41,175

Related videos on Youtube

Viktor
Author by

Viktor

Updated on July 09, 2022

Comments

  • Viktor
    Viktor almost 2 years

    I need to return generated file download as a Django REST Framework response. I tried the following:

    def retrieve(self, request, *args, **kwargs):
        template = webodt.ODFTemplate('test.odt')
        queryset = Pupils.objects.get(id=kwargs['pk'])
        serializer = StudentSerializer(queryset)
        context = dict(serializer.data)
        document = template.render(Context(context))
        doc = converter().convert(document, format='doc')
        res = HttpResponse(
            FileWrapper(doc),
            content_type='application/msword'
        )
        res['Content-Disposition'] = u'attachment; filename="%s_%s.zip"' % (context[u'surname'], context[u'name'])
        return res
    

    But it returns a msword document as json.

    How do I make it start downloading as file instead?

    • Piyush S. Wanare
      Piyush S. Wanare almost 8 years
      You mean to say that you have created an word file which you need to pass to Front End so that Front end user should able to download it?
    • Viktor
      Viktor almost 8 years
      @PiyushS.Wanare exactly
    • Owen
      Owen almost 8 years
      Maybe after the file is generated, if it's publicly accessible from your web server (without Django code, authorisation, etc) you could send a 302 Redirect response.
  • Viktor
    Viktor almost 8 years
    I don't get this line yourFilePointer.write(response,text) . My file is already generated and saved on server. What text should I write there?
  • Piyush S. Wanare
    Piyush S. Wanare almost 8 years
    Text will be the your word file text.
  • Piyush S. Wanare
    Piyush S. Wanare almost 8 years
    as we write text file like f = open('c:\file.doc', "w") f.write(text)
  • Viktor
    Viktor almost 8 years
    As I said, my file is already on disk. I don't need to write it
  • Tobias Ernst
    Tobias Ernst over 6 years
    It should be: ´response = HttpResponse(FilePointer.read(), content_type='application/msword')´
  • Xhark
    Xhark over 4 years
    How would you protect that with csrf? Acording to django document you need to use render() for it to work
  • Ryan Aquino
    Ryan Aquino almost 4 years
    what if you dont know the content_type? how to handle auto content_type
  • Aditya Sinha
    Aditya Sinha over 3 years
    But how did you made media path accessible to frontend? Like I did the same thing with path in static folder and it worked but when I did it for media folder, it said "page not found"!