开通微信小程序收费吗_node+multer完成图片上传的
摘要: node+multer完成照片提交的实例编码 本文关键详细介绍了node+multer完成照片提交的实例编码,原文中根据实例编码详细介绍的十分详尽,对大伙儿的学习培训或是工作中具备一定的参...
// 设置静态目录 第一个参数为虚拟的文件前缀,实际上文件系统中不存在 // 可以用public做为前缀来加载static文件夹下的文件了 app.use('/public', express.static(path.join(__dirname, './static'))); // 根据当前文件目录指定文件夹 const dir = path.resolve(__dirname, '../static/img'); // 图片大小限制KB const SIZELIMIT = 500000; const storage = multer.diskStorage({ // 指定文件路径 destination: function(req, file, cb) { // !!!相对路径时以node执行目录为基准,避免权限问题,该目录最好已存在* // cb(null, './uploads'); cb(null, dir); // 指定文件名 filename: function(req, file, cb) { // filedname指向参数key值 cb(null, Date.now() + '-' + file.originalname); const upload = multer({ storage: storage app.post('/upload', upload.single('file'), (req, res) = { // 即将上传图片的key值 form-data对象{key: value} // 检查是否有文件待上传 if (req.file === undefined) { return res.send({ errno: -1, msg: 'no file' const {size, mimetype, filename} = req.file; const types = ['jpg', 'jpeg', 'png', 'gif']; const tmpTypes = mimetype.split('/')[1]; // 检查文件大小 if (size = SIZELIMIT) { return res.send({ errno: -1, msg: 'file is too large' // 检查文件类型 else if (types.indexOf(tmpTypes) 0) { return res.send({ errno: -1, msg: 'not accepted filetype' // 路径可根据设置的静态目录指定 const url = '/public/img/' + filename; res.json({ errno: 0, msg: 'ess', app.listen(3000, () = { console.log('service start');
附上文档参考链接:
最后再附赠一个node自动重启工具
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持凡科。