nodejs + express + 上传图片功能
1.前台表单
<form method="post" enctype ="multipart/form-data"> <input type="file" name="pics"/> <input type="submit" value="提交" /> </form>
2.路由(routes/index.js)
app.post('/upload',function (req,res){ var _files = req.files.pics; var item ,_name ,_tmp; item = _files, _name=item.name; if (_name && item.path) {//这里需要判断文件名称和路径是否为空 var tmpPath = item.path, type = item.type ,extension_name = '', tmp_name = (Date.parse(new Date()) / 1000) + '' + (Math.round(Math.random() * 9999));//生成随机名称 switch (type) { //判断文件类型 case 'image/pjpeg': extension_name = 'jpg'; break; case 'image/jpeg': extension_name = 'jpg'; break; case 'image/gif': extension_name = 'gif'; break; case 'image/png': extension_name = 'png'; break; case 'image/x-png': extension_name = 'png'; break; case 'image/bmp': extension_name = 'bmp'; break; default: if(_name.indexOf('.')<=0) return;//其他文件则默认上传 else { _tmp = _name.split('.'); extension_name = _tmp[_tmp.length-1]; break; } } tmp_name = tmp_name + '.' + extension_name, targetPath = 'public/upload/' + tmp_name,//设置上传路径 is = fs.createReadStream(tmpPath), os = fs.createWriteStream(targetPath); util.pump(is, os, function() { fs.unlinkSync(tmpPath); console.log('upload success : ',targetPath); res.json({//设置返回值 error : 0, url : 'upload/' + tmp_name, title : tmp_name, message : tmp_name }); }); }; });
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。