nodejs --- formidable模块 , post 上传.

2021-06-07 13:02

阅读:543

标签:lower   use   cas   mit   images   ret   class   form   html   

 

 

1. 只有一个文件域:

 1 var formidable = require(‘formidable‘),
 2     http = require(‘http‘),
 3     util = require(‘util‘);
 4 
 5 http.createServer(function(req, res) {
 6   if (req.url == ‘/upload‘ && req.method.toLowerCase() == ‘post‘) {
 7     // parse a file upload
 8     var form = new formidable.IncomingForm();
 9     form.parse(req, function(err, fields, files) {
10       res.writeHead(200, {‘content-type‘: ‘text/plain‘});
11       res.write(‘received upload:\n\n‘);
12       res.end(util.inspect({fields: fields, files: files}));
13     });
14     return;
15   }
16 
17   // show a file upload form
18   res.writeHead(200, {‘content-type‘: ‘text/html‘});
19   res.end(
20
21 ‘method="post">‘+ 22 23
‘+ 24‘+ 25
26 ); 27 }).listen(8888);

技术分享

技术分享

 

 2. 多文件上传:

   不可以,最后一个选择的覆盖住了之前的.

 

 

 

3. 两个不同的文件域:

 1 var formidable = require(‘formidable‘),
 2     http = require(‘http‘),
 3     util = require(‘util‘);
 4 
 5 http.createServer(function(req, res) {
 6   if (req.url == ‘/upload‘ && req.method.toLowerCase() == ‘post‘) {
 7     // parse a file upload
 8     var form = new formidable.IncomingForm();
 9     form.parse(req, function(err, fields, files) {
10       res.writeHead(200, {‘content-type‘: ‘text/plain‘});
11       res.write(‘received upload:\n\n‘);
12       res.end(util.inspect({fields: fields, files: files}));
13     });
14     return;
15   }
16 
17   // show a file upload form
18   res.writeHead(200, {‘content-type‘: ‘text/html‘});
19   res.end(
20
21 ‘method="post">‘+ 22 23name="upload01" >
‘+ 24name="upload02">
‘+ 25‘+ 26
27 ); 28 }).listen(8888);

 

技术分享

 

 

 

技术分享

 

 

 

 

4. 除去文件域 , 有其他的 字段.

 

 1 var formidable = require(‘formidable‘),
 2     http = require(‘http‘),
 3     util = require(‘util‘);
 4 
 5 http.createServer(function(req, res) {
 6   if (req.url == ‘/upload‘ && req.method.toLowerCase() == ‘post‘) {
 7     // parse a file upload
 8     var form = new formidable.IncomingForm();
 9     form.parse(req, function(err, fields, files) {
10       res.writeHead(200, {‘content-type‘: ‘text/plain‘});
11       res.write(‘received upload:\n\n‘);
12       res.end(util.inspect({fields: fields, files: files}));
13     });
14     return;
15   }
16 
17   // show a file upload form
18   res.writeHead(200, {‘content-type‘: ‘text/html‘});
19   res.end(
20
21 ‘method="post">‘+ 22name="userName" >
‘+ 23name="password" >
‘+ 24name="upload01" >
‘+ 25name="upload02">
‘+ 26‘+ 27
28 ); 29 }).listen(8888);

 

 

 技术分享

 

技术分享

 

nodejs --- formidable模块 , post 上传.

标签:lower   use   cas   mit   images   ret   class   form   html   

原文地址:http://www.cnblogs.com/cbza/p/7327018.html


评论


亲,登录后才可以留言!