egg icon indicating copy to clipboard operation
egg copied to clipboard

使用postman上传binary数据会报Content-Type must be multipart/*

Open bymaxchen opened this issue 7 years ago • 10 comments

请问这是功能缺陷还是处于什么目的不允许直接接受二进制数据呢?

相关环境信息

  • 操作系统: windows 10
  • Node 版本: 8.9.4
  • Egg 版本: 1.7.0

bymaxchen avatar Sep 18 '19 10:09 bymaxchen

image

bymaxchen avatar Sep 18 '19 10:09 bymaxchen

这是要求你加上对应的 header,可以看下 HTTP 协议

atian25 avatar Sep 18 '19 13:09 atian25

这是要求你加上对应的 header,可以看下 HTTP 协议

我想要的请求头content-type是: application/octet-stream

这个不可以支持吗?

bymaxchen avatar Sep 19 '19 06:09 bymaxchen

Hello @bymaxchen. Please provide a reproducible example following the instruction.

Issues labeled by Need Reproduce will be closed if no activities in 7 days.


@bymaxchen,请根据这个说明提供最小可复现代码。

如果在 7 天内没有进展会被自动关闭。

egg-bot avatar Sep 19 '19 07:09 egg-bot

用的this.ctx.getFileStream()? 二进制文件的话直接 this.ctx.req.pipe(fs.createWriteStream('./filename'))

supperchong avatar Sep 19 '19 08:09 supperchong

getFileStream 只支持 mulitipart

如果你是自己的 header 的话,自己操作 req 的 stream,注意要做好 stream 的异常处理。

atian25 avatar Sep 19 '19 08:09 atian25

if (this.ctx.header['content-type'] === 'application/octet-stream') {
      const stream = this.ctx.req
      try {
        const destStream = fs.createWriteStream(filePath)
        await awaitWriteStream(stream.pipe(destStream))
      } catch (err) {
        await sendToWormhole(stream)
        throw err
      }
    }

或者

const { pipeline } = require('stream')
try {
      const stream = this.ctx.req
      const destStream = fs.createWriteStream(filePath)
      await pipeline(
        stream,
        destStream,
      );
      console.log('Pipeline succeeded');
    } catch (err) {
      console.error('Pipeline failed', err);
    }

supperchong avatar Sep 19 '19 09:09 supperchong

https://github.com/mafintosh/pump 可以关注下

atian25 avatar Sep 19 '19 09:09 atian25

@atian25 https://nodejs.org/en/docs/guides/backpressuring-in-streams/ .pump is only necessary for Nodejs 8.x or earlier, as for Node 10.x or later version, pipeline is introduced to replace for pump. This is a module method to pipe between streams forwarding errors and properly cleaning up and provide a callback when the pipeline is complete.

supperchong avatar Sep 19 '19 09:09 supperchong

@atian25 https://nodejs.org/en/docs/guides/backpressuring-in-streams/ .pump is only necessary for Nodejs 8.x or earlier, as for Node 10.x or later version, pipeline is introduced to replace for pump. This is a module method to pipe between streams forwarding errors and properly cleaning up and provide a callback when the pipeline is complete.

cc @fengmk2

atian25 avatar Sep 19 '19 09:09 atian25