Skip to content

Commit

Permalink
Trigger file download
Browse files Browse the repository at this point in the history
  • Loading branch information
salimi-my committed Feb 4, 2024
1 parent d4d912a commit f96023d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
4 changes: 1 addition & 3 deletions app/api/resume/download-file/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { NextResponse } from 'next/server';
import { getDownloadUrl } from '@edgestore/react/utils';

import prismadb from '@/lib/prismadb';

Expand Down Expand Up @@ -47,9 +46,8 @@ export async function POST(req: Request) {
}

const response = await fetch(resume.pdf);
const pdfData = await response.arrayBuffer(); // Extract PDF data
const pdfData = await response.arrayBuffer();

// Create new Response object with PDF data and headers
const headers = new Headers();
headers.set('content-type', 'application/pdf');
headers.set(
Expand Down
25 changes: 24 additions & 1 deletion components/download-cv/download-cv.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,30 @@ export default function DownloadCv() {
try {
setLoading(true);

await axios.post('/api/resume/download-file', { token });
const response = await axios.post(
'/api/resume/download-file',
{ token },
{ responseType: 'blob' }
);

const url = URL.createObjectURL(new Blob([response.data]));

const a = document.createElement('a');
a.href = url;
a.download = 'salimi-resume.pdf';

// Hide the element
a.style.display = 'none';

// Trigger the download
document.body.appendChild(a);
a.click();

// Clean up the element
document.body.removeChild(a);

// Release the object URL
URL.revokeObjectURL(url);
} catch (error) {
if (error instanceof AxiosError && error.response?.data.error) {
setError(error.response.data.error);
Expand Down

0 comments on commit f96023d

Please sign in to comment.