3

I would like to serve a file sent by my API in my VueJS client. More specifically, when going on a given URL, a file (pdf, text or image) should open up if the browsers support it (like how Chrome opens PDFs for example). I would like to do it with VueJS or in pure HTML/CSS/JS.

I have an express API working, and going on the file's URL in the API opens it. It uses Express' sendfile method directly. Now, i want to mimic this behavior inside my client, without redirecting the user to my API: I want him to stay on the same domain.

Here is my API:

const express = require('express')

const router = express.Router()
router.get('/files/*', (req, res) => res.status(200).sendFile('C://mockfile.pdf'));
module.exports = router;

Going anywhere on localhost:3000/files/ opens the file inside the browser. But now I want the same behavior for my client, without redirecting the user at localhost:3000 (the VueJS app runs on localhost:8080).

For example, going on localhost:8080/mockfile.pdf would open the file directly inside the browser. I can use any library if needed (I already use axios).

How can I achieve this?

duncan
  • 31,401
  • 13
  • 78
  • 99
TheMrZZ
  • 188
  • 1
  • 11

1 Answers1

0

I believe this package is what you're looking for vue-pdf which can be downloaded and istalled with npm.

then use it in your template like this:

<pdf src="http://localhost:3000/files/mockfile.pdf" @num-pages="pageCount = $event" @page-loaded="currentPage = $event"></pdf>

See linked documentation for furthur details on how to handle events on the document.

Edwin Krause
  • 1,766
  • 1
  • 16
  • 33
  • I tried using this package. It does render the pdf by itself, but it does not use the pdf viewer built inside browsers. My API use Express' sendfile method, and this renders the pdf inside the browser directly. I just want a way to use this response in another URL. – TheMrZZ Oct 15 '18 at 14:04