Download .txt file stored on server using Hapi js

169 Views Asked by At

Folks, I am trying to create .txt file on submitting a form and storing it in /files folder. Then, onclick of Download button I should be able to download that file. I am using inert package from Hapi js to download the file. files folder is in root of the project.

This is my route handler for downloading

handler: async (req: Hapi.Request, h: Hapi.ResponseToolkit) => {
  try {
   const user = await prisma.user.findUnique({
     where: {
      id: Number(req.params.id)
     }
   });
   if (user) {
    h.file(`${__dirname}/../../files/certificate_2.txt`, {
      mode: 'attachment',
      filename: 'certificate_2.txt'
    });

    return h.redirect('/');
   }
 } catch (err) {
   console.log(err);
 }

But, I am not able to download the file on client side. Please help me to solve this issue

0

There are 0 best solutions below