node+vue实现文件上传功能
本文实例为大家分享了node+vue实现文件上传的具体代码,供大家参考,具体内容如下
*后端*
constexpress=require('express'); constRouter=express.Router(); constmulter=require('multer'); constfs=require('fs'); constpathLib=require('path'); constvideoModel=require('../../models/my_yx_app/video'); //设置视频文件存放位置 constuploadVido=multer({ dest:'uploads_yx_app/video/' }); //上传视频 Router.post('/uploadVideo',uploadVido.single('file'),(req,res)=>{ if(req.file){ letfile=req.file; letnewName=file.path+pathLib.parse(file.originalname).ext;//修改path fs.rename(file.path,newName,(err)=>{//修改path if(err){ returnres.status(200).json({ code:0, msg:'服务器繁忙!' }) }else{ returnres.status(200).json({ code:1, msg:'上传完成', title:pathLib.parse(file.originalname).name, videoUrl:'http://127.0.0.1:3001/uploads_yx_app/video/'+file.filename+pathLib.parse(file.originalname).ext }) } }) }else{ returnres.status(200).json({ code:0, msg:'服务器繁忙!' }) } });
*前端*
添加文件 {{file.name}}