本文实例为大家分享了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: 'success' }) }, 1000); } }).catch(err => { this.$message.error('服务器繁忙,请稍后重试!'); }); } }).catch(err => { this.$message.error('服务器繁忙,请稍后重试!'); }) },
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年10月01日
2024年10月01日
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】
- 李恕权.2003-回·20年全精选2CD【SONY】【WAV+CUE】