node+vue实现文件上传功能
网络编程 2021-07-04 15:02www.168986.cn编程入门
这篇文章主要介绍了node+vue实现文件上传功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了node+vue实现文件上传的具体代码,供大家参考,具体内容如下
后端
const express = require('express'); const Router = express.Router(); const multer = require('multer'); const fs = require('fs'); const pathLib = require('path'); const videoModel = require('../../models/my_yx_app/video'); //设置 视频文件存放位置 const uploadVido = multer({ dest:'uploads_yx_app/video/' }); //上传视频 Router.post('/uploadVideo',uploadVido.single('file'),(req,res)=>{ if (req.file) { let file = req.file; let newName = file.path+pathLib.parse(file.originalname).ext; //修改path fs.rename(file.path,newName,(err)=>{ //修改path if (err) { return res.status(200).json({ code:0, msg:'服务器繁忙!' }) }else { return res.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 { return res.status(200).json({ code:0, msg:'服务器繁忙!' }) } });
前端
<div class="from-contral" style="position: relative"> <!--此处name 与 uploadVido.single('file') 相同--> <input type="file" name="file" @change="changeFile" class="customStyle"> <el-button size="mini" type="primary"> 添加文件 <i class="el-icon-upload el-icon--right"></i> </el-button> <div class="zt-title-video">{{ file.name }}</div> <div class="zt-progress" v-show="percentageShow"> <el-progress :text-inside="true" :stroke-width="20" :percentage="percentage" > </el-progress> </div> </div>
js处理逻辑数据
saveData() { //上传 let that = this; let fd = new FormData(); fd.append('file', this.file); fileUpdata({ //上传文件存储在后端 method: 'post', url: '/uploadVideo', data: fd, //监听上传时间 //实现进度条 onUploadProgress(progressEvent) { that.percentageShow = true; that.percentage = parseInt(((progressEvent.loaded / progressEvent.total) 100)); } }).then(res => { if (res.data.code === 1) { //数据持久化 fileUpdata({ method: 'post', url: '/saveVideoInfo', data: { videoUrl: res.data.videoUrl, //路径 videoName: res.data.title, //标题 videoType:that.videoType, //类型 userName:localStorage.getItem('username') //那个用户上传的 } }).then(res => { if (res.data.code === 1) { setTimeout(function () { //为什么延迟,为了使进度条走完 that.$message({ message: '上传成功', type: 'suess' }) }, 1000); } }).catch(err => { this.$message.error('服务器繁忙,请稍后重试!'); }); } }).catch(err => { this.$message.error('服务器繁忙,请稍后重试!'); }) },
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指