文件服务器临时文件,使用没有临时文件的节点js将文件上载到休息文件服务器...

这是我为完成这项工作而编写的中间件(包括处理压缩文件!)。它使用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变量,它是一个流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值