这是我的表格如何连接节点js.while提交此表单
<form id="fileupload" action="/file-upload" method="POST" enctype="multipart/form-data">
<input id="fileToBeUploaded" type="file" name="thumbnail">
<input type="submit" value="upload">
</form>
服务器端
var http = require("http");
var url = require("url");
http.createServer(function(req, res) {
switch (url.parse(req.url).pathname)
{
case '/': console.log('server side methodcalled');
break;
case '/file-upload': console.log('server side method called');
break;
default:
console.log('server side method called');
break;
}
});
答案 0 :(得分:1)
在nodejs服务器程序中,您可以检查请求方法和操作。
if((request.method == 'POST') && (request.url == '/file-upload') {
.......
}
更新 -
var http = require("http");
var url = require("url");
var fs = require("fs");
http.createServer(function(req, res) {
switch (url.parse(req.url).pathname)
{
case '/':
console.log('server side methodcalled');
fs.readFile(__dirname + '/index.html',
function (err, data) {
res.writeHead(200);
res.end(data);
});
break;
case '/file-upload':
console.log('server side method called');
break;
default:
console.log('server side method called');
break;
}
}).listen(3000);
现在您可以连接到localhost:3000
答案 1 :(得分:0)
为了实现文件上传,您需要读取请求的主体并解析出各种多个部分。这可能很棘手,所以我强烈建议您使用像formidable这样的现有模块,或者在后台调用它的东西,比如connect.bodyParser()或express.bodyParser()。