Skip to content

Commit 8f99cc7

Browse files
authored
fix(download): trigger downloads programmatically to work inside cross-origin iframes (#9649)
1 parent 9d7a8fe commit 8f99cc7

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

frontend/src/plugins/layout/DownloadPlugin.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,26 @@ const DownloadButton = ({
8080
const [isLoading, setIsLoading] = useState(false);
8181

8282
const handleClick = async (e: React.MouseEvent) => {
83-
if (!data.lazy) {
83+
if (data.disabled) {
84+
e.preventDefault();
85+
e.stopPropagation();
8486
return;
8587
}
8688

87-
if (data.disabled) {
89+
e.preventDefault();
90+
91+
if (!data.lazy) {
92+
downloadByURL(data.data, data.filename ?? undefined);
8893
return;
8994
}
9095

91-
e.preventDefault();
9296
setIsLoading(true);
9397

9498
try {
9599
const loadedData = await load({});
96100
downloadByURL(
97101
loadedData.data,
98-
loadedData.filename || data.filename || "download",
102+
loadedData.filename ?? data.filename ?? undefined,
99103
);
100104
} catch (error) {
101105
toast({
@@ -114,9 +118,7 @@ const DownloadButton = ({
114118
return (
115119
<a
116120
href={data.data}
117-
download={data.filename || true}
118-
target="_blank"
119-
rel="noopener noreferrer"
121+
download={data.filename ?? ""}
120122
onClick={handleClick}
121123
className={buttonVariants({
122124
variant: "secondary",

frontend/src/utils/download.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,12 @@ export async function downloadHTMLAsImage(opts: {
175175
}
176176
}
177177

178-
export function downloadByURL(url: string, filename: string) {
178+
export function downloadByURL(url: string, filename?: string) {
179179
const a = document.createElement("a");
180180
a.href = url;
181-
a.download = filename;
181+
if (filename) {
182+
a.download = filename;
183+
}
182184
a.click();
183185
a.remove();
184186
}

0 commit comments

Comments
 (0)