diff --git a/frappe/utils/response.py b/frappe/utils/response.py index 0c18967ae5..448d43a456 100644 --- a/frappe/utils/response.py +++ b/frappe/utils/response.py @@ -7,7 +7,7 @@ import json import mimetypes import os import sys -from typing import TYPE_CHECKING, Literal +from typing import TYPE_CHECKING from urllib.parse import quote import werkzeug.utils @@ -92,6 +92,7 @@ def as_csv(): response = Response() response.mimetype = "text/csv" filename = f"{frappe.response['doctype']}.csv" + filename = filename.encode("utf-8").decode("unicode-escape", "ignore") response.headers.add("Content-Disposition", "attachment", filename=filename) response.data = frappe.response["result"] return response @@ -101,6 +102,7 @@ def as_txt(): response = Response() response.mimetype = "text" filename = f"{frappe.response['doctype']}.txt" + filename = filename.encode("utf-8").decode("unicode-escape", "ignore") response.headers.add("Content-Disposition", "attachment", filename=filename) response.data = frappe.response["result"] return response @@ -113,10 +115,11 @@ def as_raw(): or mimetypes.guess_type(frappe.response["filename"])[0] or "application/unknown" ) + filename = frappe.response["filename"].encode("utf-8").decode("unicode-escape", "ignore") response.headers.add( "Content-Disposition", frappe.response.get("display_content_as", "attachment"), - filename=frappe.response["filename"], + filename=filename, ) response.data = frappe.response["filecontent"] return response @@ -138,7 +141,8 @@ def as_json(): def as_pdf(): response = Response() response.mimetype = "application/pdf" - response.headers.add("Content-Disposition", None, filename=frappe.response["filename"]) + filename = frappe.response["filename"].encode("utf-8").decode("unicode-escape", "ignore") + response.headers.add("Content-Disposition", None, filename=filename) response.data = frappe.response["filecontent"] return response