File upload 文件上传程序给出错误

File upload 文件上传程序给出错误,file-upload,node.js,File Upload,Node.js,运行此代码时: var http=require('http'); var multipart=require(“./multipart”); var sys=需要('sys'); var server=http.createServer(函数(req,res){ log('in upload files'+req.url); 开关(请求url){ 案例“/”: 显示表格(请求、回复); 打破 案例“/upload”: 上传_文件(请求、回复); 打破 违约: 显示404(请求、回复); 打破

运行此代码时:

var http=require('http');
var multipart=require(“./multipart”);
var sys=需要('sys');
var server=http.createServer(函数(req,res){
log('in upload files'+req.url);
开关(请求url){
案例“/”:
显示表格(请求、回复);
打破
案例“/upload”:
上传_文件(请求、回复);
打破
违约:
显示404(请求、回复);
打破
}
});
服务器监听(8000);
功能显示表格(请求、回复){
res.writeHead(200,{'Content-Type':'text/html'});
res.end(
''+
''+
''+
''
);
}
函数上传文件(req、res){
log('in upload files');
请求setEncoding('binary');
变量流=新的多部分流(req);
stream.addListener('part',函数(part){
log('in upload files1');
part.addListener('body',函数(chunk){
log('in upload files2');
var progress=(stream.bytesserved/stream.bytesTotal*100).toFixed(2);
var mb=(stream.bytesTotal/1024/1024).toFixed(1);
系统打印(“上传”+mb+“mb”(+progress+”%)\015);
//如果上传的文件需要保存,则可以将块追加到文件中
});
});
stream.addListener('complete',function(){
res.writeHead(200,{'Content-Type':'text/plain'});
res.write(‘感谢您的演奏’);
res.end();
系统放置(“\n=>Done”);
});
}
功能显示_404(请求、恢复){
res.writeHead(404,{'Content-Type':'text/plain'});
res.write(‘你正在做’);
res.end();
}
上传图像后给出的错误为:

reach121@youngib:~/rahul$ node test.js
in upload files/
in upload files/upload
in upload files

/home/reach121/rahul/test.js:37
  var stream = new multipart.Stream(req);
               ^
TypeError: undefined is not a function
    at CALL_NON_FUNCTION_AS_CONSTRUCTOR (native)
    at upload_file (/home/reach121/rahul/test.js:37:16)
    at Server.<anonymous> (/home/reach121/rahul/test.js:13:7)
    at Server.emit (events.js:67:17)
    at HTTPParser.onIncoming (http.js:1108:12)
    at HTTPParser.onHeadersComplete (http.js:108:31)
    at Socket.ondata (http.js:1007:22)
    at Socket._onReadable (net.js:678:27)
    at IOWatcher.onReadable [as callback] (net.js:177:10)
reach121@youngib:~/rahul$node test.js
在上传文件中/
在上传文件/上传中
在上传文件中
/home/reach121/rahul/test.js:37
变量流=新的多部分流(req);
^
TypeError:undefined不是函数
调用非函数作为构造函数时(本机)
上传文件(/home/reach121/rahul/test.js:37:16)
在服务器上。(/home/reach121/rahul/test.js:13:7)
在Server.emit上(events.js:67:17)
在HTTPParser.onIncoming(http.js:1108:12)
在HTTPParser.onHeadersComplete(http.js:108:31)
在Socket.ondata(http.js:1007:22)
在套接字上可读(net.js:678:27)
在IOWatcher.onReadable[作为回调](net.js:177:10)

请说明出现此错误的原因。

这是因为您使用的是旧教程。多部分显然已经改变了

查看它的代码示例>


或者遵循本教程>

这是因为您使用的是旧教程。多部分显然已经改变了

查看它的代码示例>

或者按照本教程>

reach121@youngib:~/rahul$ node test.js
in upload files/
in upload files/upload
in upload files

/home/reach121/rahul/test.js:37
  var stream = new multipart.Stream(req);
               ^
TypeError: undefined is not a function
    at CALL_NON_FUNCTION_AS_CONSTRUCTOR (native)
    at upload_file (/home/reach121/rahul/test.js:37:16)
    at Server.<anonymous> (/home/reach121/rahul/test.js:13:7)
    at Server.emit (events.js:67:17)
    at HTTPParser.onIncoming (http.js:1108:12)
    at HTTPParser.onHeadersComplete (http.js:108:31)
    at Socket.ondata (http.js:1007:22)
    at Socket._onReadable (net.js:678:27)
    at IOWatcher.onReadable [as callback] (net.js:177:10)