Download a file with a Server Action
Unanswered
skve posted this in #help-forum
skveOP
I have a server action as follows (simplified):
I've tired to use
Appreciate any help thanks!
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
Mine is CSV file, I returned
result.text() to the client component and it can work. But for PDF file, it is not working.