egg icon indicating copy to clipboard operation
egg copied to clipboard

ctx.body = stream 平台差异问题

Open norsl opened this issue 5 years ago • 3 comments

先看这个代码

ctx.attachment(path.parse(filename).base)
ctx.set('Content-Type', 'application/octet-stream')
ctx.set('Content-Length', stat.size)
ctx.body = fs.createReadStream(filePath)

在Windows上面能够正常下载文件,没什么问题。

然后我同样的代码再Ubuntu上面,body的值就变成了stream对象的json形式。每个文件的内容都是这个stream对象

{
    "_readableState": {
        "objectMode": false,
        "highWaterMark": 65536,
        "buffer": {
            "head": null,
            "tail": null,
            "length": 0
        },
        "length": 0,
        "pipes": [],
        "flowing": null,
        "ended": false,
        "endEmitted": false,
        "reading": false,
        "sync": true,
        "needReadable": false,
        "emittedReadable": false,
        "readableListening": false,
        "resumeScheduled": false,
        "errorEmitted": false,
        "emitClose": true,
        "autoDestroy": false,
        "destroyed": false,
        "errored": null,
        "closed": false,
        "closeEmitted": false,
        "defaultEncoding": "utf8",
        "awaitDrainWriters": null,
        "multiAwaitDrain": false,
        "readingMore": false,
        "dataEmitted": false,
        "decoder": null,
        "encoding": null
    },
    "_events": {},
    "_eventsCount": 2,
    "path": "/js/app.9e78b476.js",
    "fd": null,
    "flags": "r",
    "mode": 438,
    "end": null,
    "autoClose": true,
    "bytesRead": 0,
    "closed": false
}

norsl avatar Dec 03 '21 08:12 norsl

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

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


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

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

egg-bot avatar Dec 03 '21 08:12 egg-bot

找到问题根源了,被自己蠢哭了

norsl avatar Dec 04 '21 13:12 norsl

用了gzip中间件。但是这个gzip我并没有写完,导致body被JSON.stringify掉了。

norsl avatar Dec 04 '21 13:12 norsl