egg icon indicating copy to clipboard operation
egg copied to clipboard

应用间转发文件,获取stream时的一个错误

Open fishjar opened this issue 8 years ago • 8 comments

写了两个应用, A应用接受上传文件,然后发送stream给B应用, 在B应用getFileStream()时出错了 注:B应用直接接受上传的文件是没有问题的

A应用相关代码:

    const stream = await ctx.getFileStream();
    const res = await ctx.curl(`${config.api}/upload`, {
      'method': 'POST',
      'dataType': 'json',
      'stream': stream,
      'headers': ctx.request.headers,
    });

B 应用相关代码:

const stream = await ctx.getFileStream(); // 执行到此出错

B 应用错误信息:

2018-03-07 15:30:02,806 ERROR 31877 A client (::ffff:127.0.0.1:42492) error [HPE_INVALID_EOF_STATE] occurred: Parse Error

难道A应用不能这样发送文件?有没其他可行方案?

fishjar avatar Mar 07 '18 07:03 fishjar

你不是 multipipe,直接 ctx.req

popomore avatar Mar 07 '18 13:03 popomore

@popomore 不是很明白,能说详细一点吗?

fishjar avatar Mar 08 '18 01:03 fishjar

应用 b 从 ctx.req 拿到手stream

popomore avatar Mar 08 '18 01:03 popomore

@popomore 多谢,我先试试

fishjar avatar Mar 08 '18 01:03 fishjar

@popomore 经过尝试,还是不知如何在B获取到A发来的stream,能否多点提示。

而且我本来是想不修改B,只修改A来达成目标。

因为客户端直接上传文件到B应用是可以的,现在只是想多一种上传途径,通过A模拟上传文件到B。

fishjar avatar Mar 08 '18 03:03 fishjar

如果不改 B 那你需要使用 multipart 来发

https://github.com/eggjs/examples/blob/master/httpclient/app/controller/httpclient.js#L85

popomore avatar Mar 08 '18 04:03 popomore

@popomore 多谢!根据你的提示已经搞定了,最后A的代码如下:

const FromStream = require('formstream'); // https://www.npmjs.com/package/formstream

// ...
    const stream = await ctx.getFileStream();
    const form = new FromStream();
    Object.keys(stream.fields).forEach(key => form.field(key, stream.fields[key])); // 用户附带参数
    form.stream(stream.fieldname, stream, stream.filename);

    const res = await ctx.curl(`${config.api}/upload`, {
      method: 'POST',
      dataType: 'json',
      stream: form,
      headers: form.headers(),
    });
// ...

fishjar avatar Mar 08 '18 05:03 fishjar

FromStream

FromStream -> FormStream,注意拼写哦

jiaxin262 avatar May 05 '19 08:05 jiaxin262