码迷,mamicode.com
首页 > Web开发 > 详细

nodejs实现单文件上传。

时间:2016-12-13 15:55:14      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:test   ima   dir   nod   文件路径   com   rename   upload   response   

 

  new了formidable的一个实例。

    formidable模块可以直接捕获当前数据流的状态并返回文件路径。

  主要使用了file事件和end事件。

var form = new formidable.IncomingForm();
form.uploadDir="/localnonobank/test/images/";
form.on(‘file‘, function(name,file){
filepath=file.path;
filename=file.name;
});
form.parse(request, function(err,fields,files){
if(err){
throw err;
};
});

form.on(‘end‘, function() {
fs.rename(filepath,form.uploadDir+filename);
response.end(JSON.stringify({name:filename,src:form.uploadDir+filename}));
});

nodejs实现单文件上传。

标签:test   ima   dir   nod   文件路径   com   rename   upload   response   

原文地址:http://www.cnblogs.com/dabingzi/p/6170106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!