Download file not working, but data is in network response.
- Dominant language
- JavaScript
- Stars
- 15.5k
- Forks
- 2k
- PR merge metrics
- No merged PRs in 30d
Description
Hello,
I am trying to generate an excel file based on a mySQL query. I am using Nodejs and Adonisjs as my backend and Vuejs and nuxt as my frontend.
Route is working well, and data is being fetch, but I am not able to download the file, I can see data on the network preview, but no option to download file.
I really would appreciate if someone could help me out with this, below you can find my code and the response in the network preview in the browser.
Many, many thanks,
Gines
`'use strict'
const Database = use('Database')
const moment = require('moment')
const excel = require('exceljs')
class DownloadController {
async store({ request, res }) {
const data = request.all()
console.log(data)
var linhas = []
var sql =
'SELECT u.nome, email, celular, YEAR(dt_nascimento) as ano, v.nome as relacao, f.id '
sql += 'FROM tb_usuario_familia f, tb_usuario u, tb_valor v '
sql += 'WHERE u.id = f.usuario_id '
sql += 'AND f.relacao = v.id '
sql += 'AND YEAR(u.dt_nascimento) IN (2010) '
sql +=
'GROUP BY u.nome, email, celular, YEAR(dt_nascimento), v.nome, f.id '
sql += 'UNION '
sql +=
'SELECT u.nome, email, celular, YEAR(dt_nascimento) as ano, v.nome as relacao, f.id '
sql += 'FROM tb_usuario_familia f, tb_usuario u, tb_valor v '
sql += 'WHERE u.id = f.usuario_id '
sql += 'AND f.relacao = v.id '
sql += 'AND f.id IN (SELECT f.id '
sql += ' FROM tb_usuario_familia f, tb_usuario u '
sql += ' WHERE u.id = f.usuario_id '
sql += ' AND YEAR(u.dt_nascimento) IN (2010) '
sql += ' GROUP BY f.id) '
sql += 'AND f.relacao = 1 '
sql +=
'GROUP BY u.nome, email, celular, YEAR(dt_nascimento), v.nome, f.id '
const result = await Database.raw(sql)
//coloca o resultado dentro de um array
result[0].forEach((element) => {
linhas.push({
nome: element.nome,
email: element.email,
celular: element.celular,
ano: element.ano,
relacao: element.relacao,
id: element.id,
})
})
console.log(linhas)
let workbook = new excel.Workbook()
let worksheet = workbook.addWorksheet(data.relatorio.nome)
worksheet.columns = [
{ header: 'Nome', key: 'nome', width: 30 },
{ header: 'Email', key: 'email', width: 30 },
{ header: 'Celular', key: 'celular', width: 25 },
{ header: 'Ano', key: 'ano', width: 10 },
{ header: 'Relacao', key: 'relacao', width: 20 },
{ header: 'Familia ID', key: 'id', width: 5 },
]
// Add Array Rows
worksheet.addRows(linhas)
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
res.setHeader(
'Content-Disposition',
'attachment; filename=' + 'tutorials.xlsx'
)
return workbook.xlsx.write(res).then(function () {
res.status(200).end()
})
}
}
module.exports = DownloadController
`

Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.