I am developing a Django REST application that allows users to upload different types of files and view them in a Vue.js frontend without having to download them to their disk.
Just to give you an idea, think of Google Drive, which allows users to preview their files online without downloading them.
When it comes to videos, something I’d like to do is for the backend to stream the video to the frontend application, instead of the frontend downloading the whole file before being able to play it.
Right now, the view I’m using to deliver files looks like this:
@action(detail=True, methods=["get"])
def download(self, request, **kwargs):
file = self.get_object().file # file is the name of a FileField of a model
if not bool(file):
return Response(status=status.HTTP_204_NO_CONTENT)
return FileResponse(
file,
as_attachment=True,
filename=os.path.split(file.name)[1],
)
This downloads the whole file in one chunk before the user can use it.
Is there a way to stream the file to the user to achieve the experience I described? I have taken a look at StreamingHttpResponse
, but I’m still unsure how I’d use it at the moment.
EDIT: after a closer inspection, I realized FileResponse
inherits from StreamingHttpResponse
. However, from inspecting the browswer activity it appears the file is downloaded in one go. Could it be I’m just testing with files that are too small?