Serving/downloading PDF file from Deno

75 views Asked by At

so I'm trying to get a litte Deno application running where I can Download a PDF. The Problem is that I always get an error that the pdf is not readable. I allready tried Googeling for about 4 hours but either my search terms are off or something else.

My Code so far is:

import { Application, Router} from 'http://deno.land/x/oak/mod.ts';

const port = 8080;

const app = new Application();
const router = new Router();

app.use(router.routes());
app.use(router.allowedMethods());

router.get("/", ({ response }: { response: any }) => {
    response.body.text = "Attach /download into the URL to start the download.";
 }) 

router.get("/download", async ({ response }: { response: any }) => {
    const filePath = "./dummy.pdf";
    const pdf = await Deno.readFile(filePath);
    console.log(pdf)
    response.headers.set('Content-Type', 'application/pdf');
    response.headers.set('Content-disposition', 'attachment; filename="dummy.pdf"');
    response.respond(pdf)
})


console.log("Server running on port", (port));

await app.listen({ port })

I run the application with: deno run --allow-net --allow-write --allow-read .\server.ts

0

There are 0 answers