您的位置:首页 > Web前端 > Node.js

nodejs小记之formidable同步图片文件上传

2016-04-07 11:05 876 查看
本文章只记录过程,不讲解原理。

下面所有的操作都在搭建好的express工程下,express配置参考前面的文章。

创建上传表单

<form action="uoload" method="post" enctype="multipart/form-data">
<input type="file" name="uoload">
<input type="submit" value="提交信息">
</form>


创建upload服务器

在路由中创建upload路由。

首先现在formidable
npm install --save formidable


配置路由

app.post("/uploadserver",function(req,res){

form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});

});


这个时候上传文件,你将跳转另一个页面,然后以json的形式返回文件的信息。

通过

fs.renameSync(file.path,"public/img/upload/pingtaicover/"+file.name);


把上传的文件更名上传到你想要的文件夹。

写在结尾

formidable插件支持多文件同时上传。

更多服务端操作请参考

formidable使用文档
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: