Skip to content

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

Added a button to download the file along with viewing it

Vinod Pald30bca881592
diff --git a/components/PDFViewer.tsx b/components/PDFViewer.tsx
index 9e10f6dc..31427fea 100644
--- a/components/PDFViewer.tsx
+++ b/components/PDFViewer.tsx
@@ -36,7 +36,6 @@ export default function PDFViewer(props: any) {
}
}, [numPages]); // monitor numPages for changes
-
function onDocumentLoadSuccess({
numPages: nextNumPages,
}: {
@@ -80,6 +79,27 @@ export default function PDFViewer(props: any) {
setPageNumber((prevPageNumber) => prevPageNumber - 1);
}
+ async function downloadfile(e: React.MouseEvent<HTMLButtonElement>) {
+ try {
+ //get file data
+ const response = await fetch(props.file);
+ const fileData = await response.blob();
+
+ //create <a/> to download the file
+ const a = document.createElement("a");
+ a.href = window.URL.createObjectURL(fileData);
+ a.download = props.name;
+ document.body.appendChild(a);
+ a.click();
+
+ //clean up used resources
+ document.body.removeChild(a);
+ window.URL.revokeObjectURL(a.href);
+ } catch (error) {
+ console.error("Error downloading file:", error);
+ }
+ }
+
async function trackPageView(duration: number = 0) {
// If this is the initial page load, don't send the request
if (isInitialPageLoad.current) {
@@ -117,7 +137,11 @@ export default function PDFViewer(props: any) {
return (
<>
- <Nav pageNumber={pageNumber} numPages={numPages} />
+ <Nav
+ pageNumber={pageNumber}
+ numPages={numPages}
+ downloadFile={downloadfile}
+ />
<div
hidden={loading}
style={{ height: "calc(100vh - 64px)" }}
@@ -169,8 +193,15 @@ export default function PDFViewer(props: any) {
);
}
-
-function Nav({pageNumber, numPages}: {pageNumber: number, numPages: number}) {
+function Nav({
+ pageNumber,
+ numPages,
+ downloadFile,
+}: {
+ pageNumber: number;
+ numPages: number;
+ downloadFile: (e: React.MouseEvent<HTMLButtonElement>) => void;
+}) {
return (
<nav className="bg-black">
<div className="mx-auto px-2 sm:px-6 lg:px-8">
@@ -183,13 +214,16 @@ function Nav({pageNumber, numPages}: {pageNumber: number, numPages: number}) {
</div>
</div>
<div className="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0">
- <div className="bg-gray-900 text-white rounded-md px-3 py-2 text-sm font-medium">
+ <div className="bg-gray-900 text-white rounded-md px-3 py-2 text-sm font-medium m-1">
<span>{pageNumber}</span>
<span className="text-gray-400"> / {numPages}</span>
</div>
+ <div className="bg-gray-900 text-white rounded-md px-3 py-2 text-sm font-medium m-1">
+ <button onClick={downloadFile}>Download</button>
+ </div>
</div>
</div>
</div>
</nav>
);
-}
\ No newline at end of file
+}
diff --git a/components/view/document-view.tsx b/components/view/document-view.tsx
index 341bc279..d03b07f8 100644
--- a/components/view/document-view.tsx
+++ b/components/view/document-view.tsx
@@ -23,7 +23,13 @@ export type DEFAULT_DOCUMENT_VIEW_TYPE = {
file: string;
};
-export default function DocumentView({ link, error }: { link: LinkWithDocument; error: any }) {
+export default function DocumentView({
+ link,
+ error,
+}: {
+ link: LinkWithDocument;
+ error: any;
+}) {
const { data: session } = useSession();
const plausible = usePlausible();
@@ -161,6 +167,7 @@ export default function DocumentView({ link, error }: { link: LinkWithDocument;
viewId={viewData.viewId}
linkId={link.id}
documentId={document.id}
+ name={document.name}
/>
</div>
);
diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.ts
index 85725de2..2659e70b 100644
--- a/pages/api/links/[id]/index.ts
+++ b/pages/api/links/[id]/index.ts
@@ -23,7 +23,7 @@ export default async function handle(
expiresAt: true,
emailProtected: true,
password: true,
- document: { select: { id: true } },
+ document: { select: { id: true, name:true } },
},
});