V
V
Vlad Volodko2020-02-05 02:35:01
Vue.js
Vlad Volodko, 2020-02-05 02:35:01

How to download file on vuejs?

Hello. Interested in such a question, how can I download the file that generated me this package for laravel maatwebsite/excel.

I send a request with vue to the laravel controller which generates an excel file for me. The question is that I can’t, what should the method return so that I can then download this file?
Back and front of the project lie on different domains

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kazarmin, 2020-02-05
@fenix163

Did not work with maatwebsite/excel package, but there is an example with phpexcel

let requestParams = {
    method: 'get',
    url: 'some-url',
    responseType: 'blob',
};

axios.request(requestParams)
    .then({data} => {
        let filename = '123' + (Math.floor(Date.now() / 1000)) + '.xlsx';
        let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }));
        let link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', filename);
        document.body.appendChild(link);
        link.click();
        link.remove();
    });

On the backend
$oSpreadsheet = new Spreadsheet();

// TODO что-то делаем с $oSpreadsheet заполняем данными 

$oWriter = IOFactory::createWriter($oSpreadsheet, 'Xlsx');
$sFilename = '123.xlsx';

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $sFilename . '"');
header('Cache-Control: max-age=0');

$oWriter->save('php://output');
exit;

A
Aetae, 2020-02-05
@Aetae

You should just get a link that is easy to follow, and the server should just give the file to this link. Vue has nothing to do with it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question