I have an endpoint returning a FileResponse. When I use a code like the following I get an error 'read of closed file' The code looks like this:
@action(detail=True, methods=['get'], url_path='download')
def get_file(self, request, pk=None):
instance = self.get_object()
fa = getattr(instance, file_location)
if fa:
with open(fa.path, 'rb') as f:
response = FileResponse(f, as_attachment=True)
return response
else:
raise FileNotFound
when I remove the with-block it works.
Do I need the with block?
I'm using postman for the request