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?