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

来源:互联网 发布:nc软件下载 编辑:程序博客网 时间:2024/05/22 16:42

本文章只记录过程,不讲解原理。
下面所有的操作都在搭建好的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使用文档

0 0
原创粉丝点击