nodejs express 上传文件

 

1,下面就是上传文件的nodejs的后台

 

var express = require('express');
var fs = require('fs');
var path = require('path');

//createServer
var app = module.exports = express.createServer();

app.configure(function(){	
	app.use(express.bodyParser({uploadDir:'c:\\aa'}));
	app.use(express.methodOverride());
	app.use(app.router);
	app.use(express.static(__dirname + '/public'));	
});


app.post('/:service?', function(req, res){    
    if (req.files && req.files.codecsv != 'undifined') {
        var temp_path = req.files.codecsv.path;
        if (temp_path) {
            fs.readFile(temp_path, 'utf-8', function(err, content) {
                //文件的内容
                console.log('content',content);
                // 删除临时文件
                fs.unlink(temp_path);
            });
        }
    }
 
});

app.listen(4000, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});

 

 

 

2,前台代码使用jquery的ajax

 

<script type="text/javascript">
		$(function(){
		    $('#file_upload').click(function(){
			   
			    var data = new FormData();
			    var files = $('#file')[0].files;
			    
			    if (files) {
			    	data.append('codecsv',files[0]);
			    }		    
			    
			    $.ajax({
			        cache: false,
					type: 'post',
					dataType: 'json',
			        url:'upload',			        
			        data : data,
			        contentType: false,
				processData: false,
			        success : function () {
			            
			        }
			    });
			});
			
		})
			
</script>
		<table style='width: 100%;height: 100%'>
			<tr>
				<td style='width:800px;text-align: center;'>
					<input id='file' type="file">
					<input id='file_upload' type="button" value="upload">
				</td>
			</tr>
		</table>
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值