Next.js Discord

Discord Forum

Download a file with a Server Action

Unanswered
skve posted this in #help-forum
Open in Discord
I have a server action as follows (simplified):

import { redirect } from "next/navigation";

export async function downloadPdf(id: Invoice["id"]): Promise<Action.Result> {
  const { accessToken } = await getUser();

  const result = await fetch('...');

  // `result` is a PDF file.

  const blob = await result.blob();

  // What goes here to redirect the user to download the file?
  // The following does NOT work:
  const url = URL.createObjectURL(blob);

  redirect(url);
}


I've tired to use createObjectURL, or pass the blob to the client in some other ways, but I can't seem to get it working. With an API route this is easy, because the Response object has an easy way to send a blob.

Appreciate any help thanks!

1 Reply

Skipjack tuna
I got same problem.
Mine is CSV file, I returned result.text() to the client component and it can work. But for PDF file, it is not working.