Как написать File Download с помощью фреймворка django Rest?

У меня есть модель с полем filefield с xls и xlsx и человеком, который загрузил его, мне нужно написать api представление в DRF, которое возвращает загрузку на front end, как может быть мое представление?

models.py

class FileuploaderView(BaseModel):
    file = models.FileField(
        upload_to='',
        validators=[FileExtensionValidator(['xls', 'xlsx'])],
    )
    uploaded_by = models.ForeignKey(
        'accounts.Person',
        related_name= '',
        null=True,
        on_delete=models.SET_NULL,
    )

views.py:

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

Это

Вам может понадобиться следующее:

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

также проверить это

Вернуться на верх