I'm currently working on a project that involves integrating Vue with a Laravel API as the back-end. One of the requirements is to be able to download PDF files from the server when a certain link is clicked. I am using axios for making GET requests, which successfully retrieves the PDF file in the response body. However, I need a way to directly download this file without any additional user interaction.
To provide a clearer understanding of the response structure, here's a visual representation: https://i.sstatic.net/Z5ozo.png (Disclaimer: A text-based example would be more practical but due to the PDF content length, it's not feasible)
Is there a method or workaround in JavaScript to achieve a direct file download without requiring the user to click on another button?
Code
// This function is triggered upon clicking a specific link
downloadFile(id) {
const specificationId = this.$route.params.specificationId;
axios
.get(`${this.$API_URL}/api/v1/suppliersmanagement/product-specifications/${specificationId}/fileupload/${id}/download`, {
headers: this.headers,
})
.then(response => {
console.log(response);
// Implement direct file download logic here..
})
.catch(error => console.log(error));
},