diff --git a/reflex/.templates/web/utils/state.js b/reflex/.templates/web/utils/state.js index 51e8c6b8d7f..a46f55d68da 100644 --- a/reflex/.templates/web/utils/state.js +++ b/reflex/.templates/web/utils/state.js @@ -161,7 +161,8 @@ export const applyEvent = async (event, socket) => { if (event.name == "_download") { const a = document.createElement("a"); a.hidden = true; - a.href = event.payload.url; + // Special case when linking to uploaded files + a.href = event.payload.url.replace("${getBackendURL(env.UPLOAD)}", getBackendURL(env.UPLOAD)) a.download = event.payload.filename; a.click(); a.remove(); diff --git a/reflex/components/core/upload.py b/reflex/components/core/upload.py index 0d8eaea03c1..a1f9df9d078 100644 --- a/reflex/components/core/upload.py +++ b/reflex/components/core/upload.py @@ -128,7 +128,7 @@ def get_upload_dir() -> Path: ) -def get_upload_url(file_path: str) -> str: +def get_upload_url(file_path: str) -> Var[str]: """Get the URL of an uploaded file. Args: @@ -139,7 +139,7 @@ def get_upload_url(file_path: str) -> str: """ Upload.is_used = True - return f"{uploaded_files_url_prefix}/{file_path}" + return Var.create_safe(f"{uploaded_files_url_prefix}/{file_path}") def _on_drop_spec(files: Var): diff --git a/reflex/components/core/upload.pyi b/reflex/components/core/upload.pyi index 8614c6617d6..bb246f69b38 100644 --- a/reflex/components/core/upload.pyi +++ b/reflex/components/core/upload.pyi @@ -40,7 +40,7 @@ def get_upload_dir() -> Path: ... uploaded_files_url_prefix: Var -def get_upload_url(file_path: str) -> str: ... +def get_upload_url(file_path: str) -> Var[str]: ... class UploadFilesProvider(Component): @overload