[Django]-How to download file using django-storage sftp?

3👍

Here is my solution:

@action(methods=['get'], detail=True)
def download(self, request, pk=None):

    try:
        obj = CustomConfigurations.objects.get(id=pk)
    except CustomConfigurations.DoesNotExist:
        raise Http404

    if SFS.exists(obj.file.name):
        file = SFS._read(obj.file.name)
        type, encoding = mimetypes.guess_type(obj.file.name)
        response = HttpResponse(file, content_type=type)
        response['Content-Disposition'] = u'attachment; filename="{filename}'.format(
            filename=obj.file.name)
        return response
    raise Http404

Leave a comment