我正在尝试构建一个在服务器上创建文件然后将该文件的内容流式传输到客户端的应用程序。
我在客户端有如下代码:
app.event('app_home_opened', async ({ event, client, context }) => {
try {
const response: AxiosResponse<fs.ReadStream> = await axios({
method: 'post',
url: `${process.env.SOME_URL}/create`,
data: {},
headers: {},
responseType: 'stream',
});
app.client.files.upload({
file: response.data,
channels: event.channel,
});
} catch (e) {
console.error(e);
}
});
在服务器上:
router.post(
'/create',
async (
req: Request<any, any, any>,
res: Response<Stream>,
next: NextFunction
) => {
try {
const stream = fs.createReadStream('path/to/some/file.csv');
stream
.on('data', function (chunk: any) {
console.log(chunk);
})
.on('end', function () {
stream.pipe(res);
next();
})
.on('close', function (err: any) {
console.log('Stream has been Closed');
});
} catch (e) {
next(e);
}
}
);
发布请求返回此错误:
(node:70966) UnhandledPromiseRejectionWarning: Error: An API error occurred: no_file_data
Typescript 编译得很好,所以我不明白我做错了什么。任何帮助将非常感激。