嗨,我不会在不接触磁盘驱动器的情况下自己处理上传流。所以,对我来说,自然选择是多方模块。
我采用了一般示例,并根据页面https://npmjs.org/package/multiparty的说明将 form.parse 更改为非回调请求。在这种情况下,磁盘不会被触及。
我的代码如下所示:
multiparty = require("multiparty")
http = require("http")
util = require("util")
# show a file upload form
http.createServer((req, res) ->
if req.url is "/upload" and req.method is "POST"
form = new multiparty.Form()
form.on 'error', (err) ->
console.log "Error received #{err}"
form.on 'aborted', ->
console.log "Aborted"
form.on 'part', (part) ->
console.log "Part"
form.on 'close', (part) ->
console.log "close received"
res.writeHead 200,
"content-type": "text/plain"
res.end "received upload:\n\n"
form.on 'progress', (bytesReceived, bytesExpected) ->
console.log "Received #{bytesReceived}, #{bytesExpected}"
form.parse req
else
res.writeHead 200,
"content-type": "text/html"
res.end "<form action=\"/upload\" enctype=\"multipart/form-data\" method=\"post\">" + "<input type=\"text\" name=\"title\"><br>" + "<input type=\"file\" name=\"upload\" multiple=\"multiple\"><br>" + "<input type=\"submit\" value=\"Upload\">" + "</form>"
).listen 8080
控制台输出如下所示:
Part
Part
Received 64983, 337353
Received 130519, 337353
Aborted
Error received Error: Request aborted
没有生成关闭事件,所以我不知道读取套接字何时结束。如果我换行:
form.parse req
至:
form.parse req, (err, fields, files) ->
res.writeHead 200,
"content-type": "text/plain"
res.write "received upload:\n\n"
res.end util.inspect(
fields: fields
files: files
)
然后一切都很好,关闭事件被调用。但是文件存储在磁盘上。控制台如下所示:
Part
Part
Received 65536, 337353
Received 131072, 337353
Received 196608, 337353
Received 262144, 337353
Received 327680, 337353
Received 337353, 337353
close received
知道有什么问题吗?