首页 > 解决方案 > 无法打开从我的网站下载的 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),不允许操作。

有人能告诉我我错过了什么吗?

谢谢!

标签: javascriptnode.jsreactjsexpressdownload

解决方案


推荐阅读