I have the following code
<a href="../../assets/all-work/proto/pdf-test.pdf" download>Download</a>
It's inside a vue component which is located in src/components/Home/Proto.vue and the file is located in src/assets/all-work/proto/pdf-test.pdf, when I click download it downloads something completely illegible, why's that and how could I fix it?
I'm going to assume you're using Vue CLI.
The default configuration generated by the Vue CLI has special handling for images. To get the same thing working for PDFs you need to configure it yourself.
Firstly, you'll need to explicitly use
requireon yourhref:In some scenarios, such as the
srcattribute of an<img>, the wrapping withrequireis performed automatically. For an<a href>it isn't.You'll then run into a Webpack loader error because Webpack has no idea what to do with a
.pdffile.To fix that you'll need to configure a
file-loaderinvue.config.js:If you don't already have a
vue.config.jsfile then you'll need to create one. It goes at the top level, next to yourpackage.json.If you do already have a
vue.config.jsthen you'll need to merge thechainWebpackproperty in, alongside your existing configuration.By default the file will be renamed to a hash based on its content. It you want to retain the original file name you can configure that instead:
More details on the
nameoption can be found at:https://webpack.js.org/loaders/file-loader/#name