diff --git a/backend/core/views.py b/backend/core/views.py index 3aa8baca9..7328c7ad3 100644 --- a/backend/core/views.py +++ b/backend/core/views.py @@ -18,6 +18,8 @@ from pathlib import Path import humanize +from django.http import StreamingHttpResponse +from wsgiref.util import FileWrapper import io @@ -2176,8 +2178,8 @@ def word_report(self, request, pk): doc.save(buffer_doc) buffer_doc.seek(0) - response = HttpResponse( - buffer_doc.getvalue(), + response = StreamingHttpResponse( + FileWrapper(buffer_doc), content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document", ) response["Content-Disposition"] = "attachment; filename=sales_report.docx" diff --git a/frontend/src/routes/(app)/(internal)/compliance-assessments/[id=uuid]/export/word/+server.ts b/frontend/src/routes/(app)/(internal)/compliance-assessments/[id=uuid]/export/word/+server.ts index 561e6af8c..a739d299d 100644 --- a/frontend/src/routes/(app)/(internal)/compliance-assessments/[id=uuid]/export/word/+server.ts +++ b/frontend/src/routes/(app)/(internal)/compliance-assessments/[id=uuid]/export/word/+server.ts @@ -3,20 +3,21 @@ import { BASE_API_URL } from '$lib/utils/constants'; import { error } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; export const GET: RequestHandler = async ({ fetch, params }) => { - const URLModel = 'compliance-assessments'; - const endpoint = `${BASE_API_URL}/${URLModel}/${params.id}/word_report/`; + const URLModel = 'compliance-assessments'; + const endpoint = `${BASE_API_URL}/${URLModel}/${params.id}/word_report/`; - const res = await fetch(endpoint); - if (!res.ok) { - error(400, 'Error fetching the Word file'); - } + const res = await fetch(endpoint); + if (!res.ok) { + error(400, 'Error fetching the Word file'); + } - const fileName = `audit-exec-summary-${new Date().toISOString()}.docx`; + const fileName = `audit-exec-summary-${new Date().toISOString()}.docx`; - return new Response(await res.blob(), { - headers: { - 'Content-Type': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', - 'Content-Disposition': `attachment; filename="${fileName}"` - } - }); + return new Response(res.body, { + headers: { + 'Content-Type': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'Content-Disposition': `attachment; filename="${fileName}"`, + 'Transfer-Encoding': 'chunked' + } + }); };