Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

fix: use blob-based downloads for reliable cross-browser file downloads

Cursor Agent48061c76c2ab
diff --git a/components/view/dataroom/document-card.tsx b/components/view/dataroom/document-card.tsx
index c18aa784..eb616d18 100644
--- a/components/view/dataroom/document-card.tsx
+++ b/components/view/dataroom/document-card.tsx
@@ -8,6 +8,7 @@ import { toast } from "sonner";
import { timeAgo } from "@/lib/utils";
import { cn } from "@/lib/utils";
+import { triggerBlobDownload } from "@/lib/utils/trigger-download";
import { fileIcon } from "@/lib/utils/get-file-icon";
import {
HIERARCHICAL_DISPLAY_STYLE,
@@ -151,27 +152,16 @@ export default function DocumentCard({
return "File downloaded successfully";
}
- // For all other files, use the iframe method
+ // For all other files, fetch as blob for reliable cross-browser download
if (contentType?.includes("application/json")) {
const data = await response.json();
- const downloadUrl = data.isDirectDownload
- ? data.downloadUrl
- : response.url;
- // Create a hidden iframe for download
- const iframe = window.document.createElement("iframe");
- iframe.style.display = "none";
- window.document.body.appendChild(iframe);
- iframe.src = downloadUrl;
+ await triggerBlobDownload(
+ data.downloadUrl,
+ data.fileName || document.name,
+ );
- // Clean up the iframe after a delay
- setTimeout(() => {
- if (iframe && iframe.parentNode) {
- window.document.body.removeChild(iframe);
- }
- }, 5000);
-
- return "Download started";
+ return "File downloaded successfully";
}
throw new Error("Unexpected response format");
diff --git a/components/view/nav.tsx b/components/view/nav.tsx
index 825d5617..5c068e91 100644
--- a/components/view/nav.tsx
+++ b/components/view/nav.tsx
@@ -18,6 +18,7 @@ import {
import { toast } from "sonner";
import { determineTextColor } from "@/lib/utils/determine-text-color";
+import { triggerBlobDownload } from "@/lib/utils/trigger-download";
import {
DropdownMenu,
@@ -179,17 +180,12 @@ export default function Nav({
}, 100);
} else {
// Handle JSON response with downloadUrl (non-watermarked files)
- const { downloadUrl } = await response.json();
+ const { downloadUrl, fileName } = await response.json();
- const link = document.createElement("a");
- link.href = downloadUrl;
- link.rel = "noopener noreferrer";
- document.body.appendChild(link);
- link.click();
-
- setTimeout(() => {
- document.body.removeChild(link);
- }, 100);
+ await triggerBlobDownload(
+ downloadUrl,
+ fileName || "document",
+ );
}
return "File downloaded successfully";
diff --git a/components/view/viewer/download-only-viewer.tsx b/components/view/viewer/download-only-viewer.tsx
index 2ff59eef..42163837 100644
--- a/components/view/viewer/download-only-viewer.tsx
+++ b/components/view/viewer/download-only-viewer.tsx
@@ -7,6 +7,7 @@ import { toast } from "sonner";
import { useSafePageViewTracker } from "@/lib/tracking/safe-page-view-tracker";
import { getTrackingOptions } from "@/lib/tracking/tracking-config";
+import { triggerBlobDownload } from "@/lib/utils/trigger-download";
import { Button } from "@/components/ui/button";
@@ -233,8 +234,12 @@ export default function DownloadOnlyViewer({
}, 100);
} else {
// Handle JSON response with downloadUrl (non-watermarked files)
- const { downloadUrl } = await response.json();
- window.open(downloadUrl, "_blank");
+ const { downloadUrl, fileName } = await response.json();
+
+ await triggerBlobDownload(
+ downloadUrl,
+ fileName || documentName || "document",
+ );
}
return "File downloaded successfully";
diff --git a/lib/utils/trigger-download.ts b/lib/utils/trigger-download.ts
new file mode 100644
index 00000000..70fcd1dd
--- /dev/null
+++ b/lib/utils/trigger-download.ts
@@ -0,0 +1,40 @@
+/**
+ * Triggers a reliable file download that works across all browsers,
+ * including iOS Safari, Chrome, and Brave.
+ *
+ * The standard approach of creating an <a> element and calling click()
+ * with a cross-origin URL fails on many browsers (especially mobile)
+ * because:
+ * 1. The `download` attribute is ignored for cross-origin URLs
+ * 2. `window.open()` is blocked by popup blockers in async callbacks
+ * 3. iOS WebKit requires user activation for navigation, which expires
+ * after an async operation like fetch()
+ *
+ * This utility fetches the file as a blob and creates a same-origin
+ * blob URL, which always respects the `download` attribute.
+ */
+export async function triggerBlobDownload(
+ url: string,
+ fileName: string,
+): Promise<void> {
+ const response = await fetch(url);
+
+ if (!response.ok) {
+ throw new Error(`Failed to fetch file: ${response.statusText}`);
+ }
+
+ const blob = await response.blob();
+ const blobUrl = URL.createObjectURL(blob);
+
+ const link = document.createElement("a");
+ link.href = blobUrl;
+ link.download = fileName;
+ link.rel = "noopener noreferrer";
+ document.body.appendChild(link);
+ link.click();
+
+ setTimeout(() => {
+ URL.revokeObjectURL(blobUrl);
+ document.body.removeChild(link);
+ }, 100);
+}
diff --git a/pages/api/links/download/index.ts b/pages/api/links/download/index.ts
index 08786476..56a1dd00 100644
--- a/pages/api/links/download/index.ts
+++ b/pages/api/links/download/index.ts
@@ -209,7 +209,9 @@ export default async function handle(
return res.send(Buffer.from(pdfBuffer));
}
- return res.status(200).json({ downloadUrl });
+ return res
+ .status(200)
+ .json({ downloadUrl, fileName: view.document!.name });
} catch (error) {
return res.status(500).json({
message: "Internal Server Error",