小编Esn*_*aky的帖子

将文件从 expressJS (sendFile) 服务器下载到 VueJS,是否返回损坏的文件?

我有一个用 expressjs 编写的 API,它在提供文件 ID 时发送一个文件。

后端工作正常,因为当直接在浏览器中输入路由 url 时,它会发送正确的文件(未损坏)。前任。http://localhost:8080/download?id=1234 (下载所有文件就好了 ie.txt, xlsx, jpg, zip)

我的快速路由实际上使用 res.download,它实际上只是 sendFile 的包装器。

当我尝试从 Vue http get 请求调用这些路由 url 时,它只返回未损坏的 txt 文件。所有其他文件下载,但由于损坏,它们可以打开。

任何人都可以指出我为什么不能在 Vue 中工作的正确方向吗?

为清楚起见,“item”作为参数传递给此函数。

        this.$http.get("http://localhost:8000/files/download", {params:{id:item._id}}, {responseType: 'arraybuffer'})
      .then(response => {

        var blob = new Blob([response.data], {type:response.headers.get('content-type')});

        var link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = item.filename;
        link.click();
      })
Run Code Online (Sandbox Code Playgroud)

供参考,这是我的快速路线

blob node.js express vue.js vuejs2

3
推荐指数
1
解决办法
2032
查看次数

标签 统计

blob ×1

express ×1

node.js ×1

vue.js ×1

vuejs2 ×1