Javascript 通过我的前端从node express服务器下载JSON文件
我已将文件上传到项目目录中的下载文件夹后存储。 我想从前端下载保存的文件。 当我点击下载按钮时,它不会获取文件 当我进入Javascript 通过我的前端从node express服务器下载JSON文件,javascript,node.js,express,frontend,Javascript,Node.js,Express,Frontend,我已将文件上传到项目目录中的下载文件夹后存储。 我想从前端下载保存的文件。 当我点击下载按钮时,它不会获取文件 当我进入http://localhost:5000/download在express应用程序上,我收到了此错误消息 错误:发送邮件后无法设置邮件头。 Express服务器代码: app.get('/download', (req, res) => { res.send('file downloaded') const file = './downloads/ou
http://localhost:5000/download
在express应用程序上,我收到了此错误消息
错误:发送邮件后无法设置邮件头。
Express服务器代码:
app.get('/download', (req, res) => {
res.send('file downloaded')
const file = './downloads/output.yml';
res.download(file, 'openapi.yml', (err) => {
if (err) {
console.log(err)
} else {
console.log('file downloaded')
}
});
});
<button class="download-btn">download</button>
const handleDownload = async () => {
const res = await fetch("https://cors-anywhere.herokuapp.com/http://localhost:5000/download");
const blob = await res.blob();
download(blob, 'output.yml');
}
downloadBtn.addEventListener('click', handleDownload);
前端应用程序代码:
app.get('/download', (req, res) => {
res.send('file downloaded')
const file = './downloads/output.yml';
res.download(file, 'openapi.yml', (err) => {
if (err) {
console.log(err)
} else {
console.log('file downloaded')
}
});
});
<button class="download-btn">download</button>
const handleDownload = async () => {
const res = await fetch("https://cors-anywhere.herokuapp.com/http://localhost:5000/download");
const blob = await res.blob();
download(blob, 'output.yml');
}
downloadBtn.addEventListener('click', handleDownload);
HTML:
app.get('/download', (req, res) => {
res.send('file downloaded')
const file = './downloads/output.yml';
res.download(file, 'openapi.yml', (err) => {
if (err) {
console.log(err)
} else {
console.log('file downloaded')
}
});
});
<button class="download-btn">download</button>
const handleDownload = async () => {
const res = await fetch("https://cors-anywhere.herokuapp.com/http://localhost:5000/download");
const blob = await res.blob();
download(blob, 'output.yml');
}
downloadBtn.addEventListener('click', handleDownload);
文件夹结构:
app.get('/download', (req, res) => {
res.send('file downloaded')
const file = './downloads/output.yml';
res.download(file, 'openapi.yml', (err) => {
if (err) {
console.log(err)
} else {
console.log('file downloaded')
}
});
});
<button class="download-btn">download</button>
const handleDownload = async () => {
const res = await fetch("https://cors-anywhere.herokuapp.com/http://localhost:5000/download");
const blob = await res.blob();
download(blob, 'output.yml');
}
downloadBtn.addEventListener('click', handleDownload);
更新:
Server.js
const uploadFiles = async (req, res) => {
const file = await req.files[0];
console.log(file)
postmanCollection = file.path;
outputFile = `downloads/${file.filename}.yml`
convertCollection();
res.json({ message: "Successfully uploaded files" });
}
app.post("/upload_files", upload.array("files"), uploadFiles);
任何人请帮我解决这个问题。您已经在使用
res.send
,它会将响应头发送回客户端,从而结束请求-响应周期,当您尝试执行res.download
时,它会抛出错误。改用
app.get('/download', (req, res) => {
const file = './downloads/output.yml';
res.download(file, 'openapi.yml', (err) => {
if (err) {
console.log(err)
} else {
console.log('file downloaded')
}
});
});
res.send('file downloaded')
-->删除此行
您还需要更新js代码
const handleDownload = async () => {
const res = await fetch("https://cors-anywhere.herokuapp.com/download"); //http://localhost:5000--->this is not required
const blob = await res.blob();
download(blob, 'output.yml');
}
downloadBtn.addEventListener('click', handleDownload);
你好,谢谢你的帮助,现在我可以下载文件时,我点击这个。但是,当我点击前端的下载按钮时,它仍然会显示相同的错误消息并下载一个空文件脚本。js:24 GET 403(禁止)参见updates@RishiPurwarNow,它会在前端显示此错误消息“加载资源失败:服务器以403(禁止)的状态响应”后端上的此错误消息“node:19280)unhandledPromisejectionWarning:TypeError:无法读取未定义的“我看不到
path
问题中的任何地方请更新问题我更新问题,请检查