Node.js文件上传代码
var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var fs = require("fs"); var multer = require('multer'); var iconv = require('iconv-lite'); var filelist = require('./filelist'); var urlencodedParser = bodyParser.urlencoded({extended:false}); app.use(multer({ dest: '/tmp/'}).array('p_w_picpath')); app.use(express.static('public')); app.get('/',function(req,res){ res.sendFile(__dirname + "/public/" + "h2.html"); }); app.get("/filelist",function(req,res){ console.log("GET filelist"); var txt = filelist.fileList('/books'); res.writeHead(200,{'Content-Type':'text/html'}); res.end(txt); }) app.post("/process_post",urlencodedParser,function(req,res){ var txt = "这是一个测试页面 姓名:"+ req.body.first_name+"
年龄:"+ req.body.age+""; /*var response={ "first_name":req.body.first_name, "age":req.body.age };*/ //console.log(response); res.writeHead(200, {'Content-Type': 'text/html'}); res.end(txt); }); app.post('/file_upload', function (req, res) { console.log(req.files[0]); // 上传的文件信息 var des_file = __dirname + "/books/" + req.files[0].originalname; fs.readFile( req.files[0].path, function (err, data) { fs.writeFile(des_file, data, function (err) { if( err ){ console.log( err ); }else{ response = { message:'File uploaded successfully', filename:req.files[0].originalname }; } console.log( response ); var txt = "这是一个测试页面 文件名:"+ response.filename+""; res.writeHead(200, {'Content-Type': 'text/html'}); res.end(txt); //res.end( iconv.encode(JSON.stringify( response.filename ) ,'utf-8').toString()); }); }); }); var server = app.listen(80,'localhost',function(){ //如果不指定主机名'localhost',默认是 IPv6 var host = server.address().address; var port = server.address().port; //console.log(host); console.log("访问地址:http://%s:%s",host,port); }); // 终端打印如下信息 console.log('Server running at http://127.0.0.1:80/');
相关的H1.html文件内容:
成都创新互联公司是专业的红桥网站建设公司,红桥接单;提供成都做网站、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行红桥网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
这是一个测试页面
文件上传:
选择一个文件上传:文件浏览
新闻标题:Node.js文件上传代码
分享路径:http://myzitong.com/article/igeoei.html