0

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

Filip Seman
  • 1,252
  • 2
  • 15
  • 22
OsMos
  • 1
  • With you code I only get 500 internal server error. Works when changed to `router.get("/download", async (ctx) => { const filePath = "./dummy.pdf"; const pdf = await Deno.readFile(filePath); ctx.response.body = pdf; ctx.response.headers.set('Content-Type', 'application/pdf'); ctx.response.headers.set('Content-disposition', 'attachment; filename="dummy.pdf"');` – jps Apr 27 '21 at 18:14
  • Can you provide error message? If you have a repository, also provide a link so we can reproduce the issue. – Filip Seman May 11 '21 at 06:50

0 Answers0