egg icon indicating copy to clipboard operation
egg copied to clipboard

getFileStream 返回的是FileStream 不是ReadStream

Open zuimeiaj opened this issue 9 years ago • 21 comments

我使用的是egg-ci 1.6.0的。 在上传文件的时候,打印出获取到的文件流是FileStream ,但是我看注释上写的返回是ReadStream,我知道是我使用上的问题,但是我找了很久都没找到解决方案,刚刚接触这个,希望能提供带你解决方案

zuimeiaj avatar Jul 31 '17 14:07 zuimeiaj

请按照issue模板提供信息

ngot avatar Aug 01 '17 03:08 ngot

需要补充下信息

atian25 avatar Aug 03 '17 00:08 atian25

ReadStream 的子类

Sent from my iPhone

On 3 Aug 2017, at 8:44 AM, TZ | 天猪 [email protected] wrote:

需要补充下信息

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

fengmk2 avatar Aug 03 '17 01:08 fengmk2

找到答案了吗?我也遇到了,

guhong1023 avatar Aug 24 '17 13:08 guhong1023

补一下 polymorphism

ngot avatar Aug 24 '17 13:08 ngot

@zuimeiaj 能给个例子看看嘛?

guhong1023 avatar Aug 25 '17 01:08 guhong1023

same issue

场景: NodeJS接受客户端上传文件,然后上传到OSS。

ctx.getFileStream() 获取到的是FileStream,

fs.createReadStream(__filename) 获取到的是ReadStream。

 request.post({
     url: 'oss_url',
     formData: {
        filecontent: {
          value: ossStream,
          options: {
            filename: 'test',
            contentType: 'image/png',
          }
        }
     },
 }

ossStream 为 ctx.getFileStream() 时,上传失败, ossStream 为 fs.createReadStream(__filename) 时,上传成功。

求问这两者有何区别,如何解决。

TinChak avatar Sep 22 '17 07:09 TinChak

还有一个问题, ctx.curl 如何实现 上述request.post的表单上传文件功能? 试过文档里 以 Multipart 方式上传文件, 但公司OSS还是会返回file is not specified in multipart

TinChak avatar Sep 22 '17 07:09 TinChak

@ngot @atian25 @fengmk2

TinChak avatar Sep 22 '17 07:09 TinChak

yield ctx.curl(url, {
  method: 'POST',
  stream: ossStream,
});

popomore avatar Sep 22 '17 08:09 popomore

@popomore 就是这么写的,报错。可能上传的字段里必须包括filecontent,不知道用egg curl怎么写?

TinChak avatar Sep 22 '17 15:09 TinChak

https://www.npmjs.com/package/form-data

用这个组装一个 stream 试试

popomore avatar Sep 22 '17 16:09 popomore

怎么才能获取到ReadStream

wuyunjiang avatar Jun 12 '18 11:06 wuyunjiang

@cuitianze 您解决了吗,我现在遇到跟你一样的问题,我需要将一张网络图片(只有链接),读下来传给识别接口,也是只有createReadStream可以,这种怎么解决

wuyunjiang avatar Jul 02 '18 11:07 wuyunjiang

天啦噜,到底怎么才能得到ReadStream啊 @atian25 @popomore ,用form-data组装也不行啊

wuyunjiang avatar Sep 06 '18 06:09 wuyunjiang

https://github.com/eggjs/examples/tree/master/multipart/app/controller

atian25 avatar Sep 06 '18 06:09 atian25

我不想让他保存为临时文件去读取,没有直接的办法吗,接收文件的服务端只接受readstream

wuyunjiang avatar Sep 06 '18 07:09 wuyunjiang

你需要学习下 Node Stream 的相关知识,FileStream 是 ReadStream 的子类,是可以直接传过去的。

atian25 avatar Sep 06 '18 07:09 atian25

接受文件的接口是微信那边提供的,那边我没法控制。fileStream直接传过去不可以呀

wuyunjiang avatar Sep 06 '18 07:09 wuyunjiang

用 egg-init --type=simple 初始化个最小可复现仓库,然后提交到你的 GitHub 吧

atian25 avatar Sep 06 '18 07:09 atian25