这是我为完成这项工作而编写的中间件(包括处理压缩文件!)。它使用multiparty,因此如果您想使用它,则必须npm install。
multiparty = require("multiparty")
zlib = require("zlib")
stream_file_upload = (req, res, next) ->
# Create the formidable form
form = new multiparty.Form()
needed_parts = 0
succeeded_parts = 0
form.on "part", (part) ->
needed_parts += 1
if part.filename
# Handle unzipping
unzipper = null
if /\.zip$/.exec(part.filename)
unzipper = zlib.createUnzip()
else if /\.gz$/.exec(part.filename)
unzipper = zlib.createGunzip()
if unzipper
part.pipe(unzipper)
req.file = unzipper or part
next() if needed_parts == succeeded_parts += 1
else
# Need to wait for these to get parsed before next
val = ""
part.on "data", (data) ->
val += data
part.on "end", () ->
req.body[part.name] = val
next() if needed_parts == succeeded_parts += 1
form.parse req
中间件添加了一个req.file变量,它是一个流。