javascript - 无法打开从我的网站下载的 zip 文件
问题描述
我需要从我的 node js express 应用程序下载一个 zip 文件。这是我在服务器上用来将文件发送到浏览器的代码:
app.get('/files', async (req, res) => {
try {
const file = await service.getFile('http://localhost:8080/myapp/test.zip');
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-disposition', 'attachment; filename=test1.zip');
res.send(file);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
Web客户端(React)的代码是:
import {Button} from 'semantic-ui-react';
const axios = require('axios');
const FileDownload = require('js-file-download');
const handleClick = async () => {
const {data} = await axios({
method: 'get',
url: 'http://localhost:4000/files',
headers: {'Authorization': 'XXXXXX'},
responseType: 'stream'
});
FileDownload(data, 'file.zip');
}
export default () => <div><Button onClick={() => handleClick()}>Download file</Button></div>
zip正确下载,我可以看到大小实际上是服务器源文件的大小,但是当我尝试在Mac上解压缩时,我得到:错误(1),不允许操作。
有人能告诉我我错过了什么吗?
谢谢!
解决方案
推荐阅读
- java - 使用节点类的基本单链表实现的缺点
- linux - 为什么 TCP 快速重传不适用于 3 个重复数据包?
- optimization - 用于数学优化的 Google AI Platform
- reactjs - 子组件未在 React.js 功能组件中更新父级的状态
- javascript - 如何使用 JSDoc 向类中的函数添加参数
- c - firebase auth rest api没有在arduino ide中返回全身响应
- kubernetes - 使用 Redis Kubernetes 解析服务器
- php - PHP 日期时间转换为 ISO 8601
- python - 高效的多窗口 tkinter
- angular-cli - 为什么'ng'在GitBash中一直显示'command not found'?