Javascript 有人知道为什么res.download每次都给我的下载文件取一个随机名称吗?
我使用的是express 4.17.1。当我尝试使用res.download将csv文件发送到浏览器时,文件已下载,但文件名如下:3d6a8bc1-696c-40f2-bae8-29ca69658534.csv 然后,当我再次尝试下载同一文件时,它将以以下名称发送该文件:c1cd40ff-ea9d-4327-9389-9768fb53384a.csv 每次都是不同的随机字符串 我的代码是这样的:Javascript 有人知道为什么res.download每次都给我的下载文件取一个随机名称吗?,javascript,node.js,express,download,Javascript,Node.js,Express,Download,我使用的是express 4.17.1。当我尝试使用res.download将csv文件发送到浏览器时,文件已下载,但文件名如下:3d6a8bc1-696c-40f2-bae8-29ca69658534.csv 然后,当我再次尝试下载同一文件时,它将以以下名称发送该文件:c1cd40ff-ea9d-4327-9389-9768fb53384a.csv 每次都是不同的随机字符串 我的代码是这样的: res.download(filePath, 'list.csv'); 文件路径如下:./down
res.download(filePath, 'list.csv');
文件路径如下:./downloadables/mail-list-14da.csv
我尝试过使用sendFile,但得到了相同的结果。我最近更新了以前版本的express,看看它是否能自动解决这个问题,但它仍在这样做
编辑:根据要求编辑以下更多代码
以下是整个请求端点:
/*
* Download the list specified by the list_id with the appropriate fields as specified by the
* list_type parameter.
*/
router.get('/download/:list_type/:list_id', authCheck('list'), function(
req,
res,
next
) {
let listData = {};
Voter.aggregate(aggrPipelineList(req.params.list_type, req.params.list_id))
.allowDiskUse(true)
.exec()
.then(voterDocs => {
if (voterDocs && voterDocs.length === 0) {
res.status(404).json({
message: `list_id ${req.params.list_id} not found`
});
} else {
listData.voter_docs = voterDocs;
return req.params.list_type;
}
})
.then(listType => {
if (listType === 'mail') {
return generateMailExportFile(req.params.list_id, listData);
} else if (listType == 'phone') {
return generateCallExportFile(req.params.list_id, listData);
} else {
return generateFacebookExportFile(req.params.list_id, listData);
}
})
.then(filePath => {
console.log('FP: ' + filePath);
res.download(filePath, 'list.csv');
})
.catch(err => {
res.status(500).json({ message: err.message }); // @note: added message
});
});
为了完整,还包括GenerateEmailExportFile函数。是的,我知道我可以重构三个生成导出文件的函数。在我的名单上。。。在我知道自己到底在做什么之前,我就开始写这篇文章了
generateMailExportFile = function(listID, listData) {
let fields = [
'First Name',
'Last Name',
'Suffix',
'Street Address 1',
'Street Address 2',
'City',
'State',
'Zip'
];
let fileName = 'mail-list-' + listID.substr(listID.length - 4) + '.csv';
let voterRows = buildVoterRowsForMailList(listData.voter_docs);
let csv = json2csv({ data: voterRows, fields: fields });
let tempServerFilePath = './downloadables/' + fileName;
return new Promise((resolve, reject) => {
fs.writeFile(tempServerFilePath, csv, function(err) {
if (err) {
reject(err);
} else {
resolve(tempServerFilePath);
}
});
});
};
以下是请求文件下载的redux/thunk函数:
export const downloadList = (listId, type) => {
return (dispatch, getState) => {
const rshttp = new RSHttp(getState);
rshttp
.get('/list/download/' + type + '/' + listId)
.then(response => {
let file = new Blob([response.data], { type: 'text/csv' }),
url = window.URL.createObjectURL(file);
window.open(url);
})
.catch(error => {
console.log('Error Downloading File: ' + JSON.stringify(error));
});
};
};
我以前没有想到这个问题在我这边。如果我找到答案,我会更新这个问题。任何想法都非常感谢 问题是您正在前端重新创建文件。您只需将
React
代码更改为:
export const downloadList = (listId, type) => {
return (dispatch, getState) => {
window.open('http://localhost:8080/list/download/' + type + '/' + listId', '_blank')
// Replace with your backend URL :)
};
};
它将下载文件名正确的
文件
行在我看来很好,应该可以根据文档工作,可能是其他地方的错误?可能是客户端问题?浏览器中的代码是什么?能否向我们展示该请求处理程序的更多代码?否则很难理解会发生什么。