表达js sendfile并在react客户端

时间:2018-05-24 21:24:51

标签: javascript reactjs express

我将js表示为后端并将js作为前端反应,在某些路由中我必须发送文件以响应请求我已经这样做了:

return res.status(200).sendFile(path.resolve(`files/${product.fileName}`))

我所遇到的问题是我应该如何在reactjs客户端处理这个响应? 我得到了回应,身体是:

body : ReadableStream 
       locked : true

1 个答案:

答案 0 :(得分:1)

你能使用像这样的发送实用程序吗?

 var fs = require('fs-extra'); // import fs-extra package
 var buffer = fs.readFileSync(req.query.filepath);
 var bufferBase64 = new Buffer(buffer);
 res.status(200).send(bufferBase64);

在React客户端中,您可以使用react-file-download软件包。

import fileDownload from 'react-file-download';
fileDownload(response.data, "profilepic.jpg"); //This will download the file in browser.