fileupload = (req, res, uploadEndpoint) ->
req.connection.setTimeout 10000
poster = request.post(uploadEndpoint, (err, response, body) ->
console.log err + ":" + response.statusCode + ":" + body
jsonbody = JSON.parse(body)
console.log "jsonbody: " + JSON.stringify(jsonbody)
console.log "Error ofcourse" if jsonbody.error isnt `undefined`
)
req.pipe(poster).pipe res
这是一个acoffee脚本片段。我正在使用它将文件上传到另一个基于休息的文件服务器。当文件很大时,这不起作用。这给了我这个错误。 Expressjs在没有bodyparser
的情况下使用,但app.use express.json() app.use express.urlencoded()
使用了这两个。我不想使用临时文件来避免昂贵的i / o。看起来像是管道问题。有什么见解吗?
{"error":"multipart: Part Read: read tcp 192.168.1.1:49688: i/o timeout"}
答案 0 :(得分:2)
这是我为完成这项工作而编写的中间件(包括处理压缩文件!)。它使用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
变量,它是一个流。