Skip to content

Commit

Permalink
[REF-2157] Allow rx.download to resolve rx.get_upload_url links (r…
Browse files Browse the repository at this point in the history
  • Loading branch information
masenf authored and Malte Klemm committed Mar 12, 2024
1 parent 60bd0a0 commit 85fa6ef
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion reflex/.templates/web/utils/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions reflex/components/core/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/core/upload.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 85fa6ef

Please sign in to comment.