vue vantUI实现文件(图片、文档、视频、音频)上传(多文件)


Posted in Javascript onOctober 15, 2019

说在前面

网上有很多种文件上传的方法,根据公司最近的业务需求,要求实现多种文件的上传,在实现过程中,查阅了很多资料,最后,终于把功能实现了,开心!

<template>
  <div>
    <v-header :left-text="`上传${columnName}`"></v-header>

    <div class="upload">
      <div v-if="columnName === '视频'">
        <div class="forPreview_video" v-for="(item, index) in uploadVideoList" :key="index">
          <video :src="videoSrc"></video>
          <van-icon name="delete" @click="delBtn(index)" class="delte"/>
        </div>
        <van-uploader v-show="uploadVideoList.length < 2" preview-size="80px" accept="video/*" :before-read="beforeRead" :after-read="afterRead"></van-uploader>
      </div>

      <div v-if="columnName === '文档'">
        <div class="forPreview_doc" v-for="(item, index) in uploadDocList" :key="index">
          <img src="../../assets/img/resource_doc_b@2x.png" alt="">
          <span>{{item.name}}</span>
          <span>{{item.size | formatSize}}</span>
          <van-icon name="delete" @click="delBtn(index)" class="delte"/>
        </div>
        <van-uploader v-show="uploadDocList.length < 2" preview-size="80px" accept=".doc, .docx, .xml, .xlsx, .pdf" :before-read="beforeRead" :after-read="afterRead"></van-uploader>
      </div>

      <div v-if="columnName === '音频'">
        <div class="forPreview_audio" v-for="(item, index) in uploadAudioList" :key="index">
          <img src="../../assets/img/resource_audio@2x.png" alt="">
          <span>{{item.name}}</span>
          <span>{{item.size | formatSize}}</span>
          <van-icon name="delete" @click="delBtn(index)" class="delte"/>
        </div>
        <van-uploader v-show="uploadAudioList.length < 2" preview-size="80px" accept="audio/*" :before-read="beforeRead" :after-read="afterRead"></van-uploader>
      </div>

      <div v-if="columnName === '图片'">
        <div class="forPreview_pic" v-for="(item, index) in uploadPicList" :key="index">
          <img :src="picSrc" alt="">
          <van-icon name="delete" @click="delBtn(index)" class="delte"/>
        </div>
        <van-uploader v-show="uploadPicList.length < 2" accept="image/*" preview-size="80px" :before-read="beforeRead" :after-read="afterRead"></van-uploader>
      </div>

      <div class="diy-submit">
        <van-button @click="doSubmit($event)">提交</van-button>
      </div>
    </div>
  </div>
</template>

上述html代码是在同一页面根据前一页面传回的columnName的值来判断需要上传哪种类型文件。

<script>
  import VHeader from '../../common/header'
  import {classifyList, uploadFile, addResources} from '../../http/mock'
  import Toast from 'vant'
  export default {
    name: "uploadFile",
    components: {
      VHeader
    },
    data(){
      return{
        tagList: [], 
        uploadTitle: '',
        currentTag: null,
        tagId: null,
        columnName: localStorage.getItem('columnName'),
        fileIdArr: [],

        uploadVideoList: [], // 选中的上传视频列表
        videoSrc: '',  // 选中的视频的src,用来显示视频
        uploadDocList: [], // 选中的上传文档列表
        uploadAudioList: [], // 选中的上传音频列表
        uploadPicList: [], // 选中的上传图片列表
        picSrc: '', // 选中的图片的src,用来显示图片缩略图
      }
    },
    filters: {
      formatSize(val) { // 格式化文件大小
        if (val > 0) {
          return (val / 1024 / 1024).toFixed(2) + 'M';
        } else {
          return '0M';
        }
      },
    },
    methods: {
      // vant上传文件前校验文件事件
      // 文件选中后先提交给后台,后台根据选中的文件,返回数组(这一业务根据后台而定)
      beforeRead(file){
        let formData = new FormData(); // 为上传文件定义一个formData对象
        let config = {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        };
        let uploadUrl = URL.createObjectURL(file); // 将选中的上传文件转化为二进制文件,显示在页面上
        if(this.columnName === '视频'){
          this.uploadVideoList.push(file);
          this.videoSrc = uploadUrl;
          // 这里使用foreach是为了将选中的多个文件都添加进定义的formdata变量中
          this.uploadVideoList.forEach(item => {
            formData.append(item.name, item)
          })
        }else if(this.columnName === '文档'){
          this.uploadDocList.push(file);
          this.uploadDocList.forEach(item => {
            formData.append(item.name, item)
          })
        }else if(this.columnName === '音频'){
          this.uploadAudioList.push(file);
          this.uploadAudioList.forEach(item => {
            formData.append(item.name, item)
          })
        }else if(this.columnName === '图片'){
          this.uploadPicList.push(file);
          this.picSrc = uploadUrl;
          this.uploadPicList.forEach(item => {
            formData.append(item.name, item)
          })
        }


        // formData.append(file.name, file); // 单个文件上传时可以这么写,上面的foreach就可以删掉
        this.$api.post(uploadFile, formData, config).then(res => {
          this.fileIdArr = res.data.data;  // 把?中的文件?魉徒o後台
        }).catch(err => {
          Toast('文件上?魇?。?)
        })
      },
      // 删除待上传的文件
      delBtn(index){
        if(this.columnName === '视频'){
          // 先判断当前的选中的索引是否是在有效范围中,如果不是则跳出方法 
          if(isNaN(index) || index >= this.uploadVideoList.length){
            return false;
          }
          let tmp = [];
          // 将没被选中的上传文件存放进一个临时数组中
          for(let i = 0; i < this.uploadVideoList.length; i++){
            if(this.uploadVideoList[i] !== this.uploadVideoList[index]){
              tmp.push(this.uploadVideoList[i]);
            }
          }
          // 存放当前未被选中的上传文件
          this.uploadVideoList = tmp;
        }

        if(this.columnName === '文档'){
          console.log(this.uploadDocList.length)
          if(isNaN(index) || index >= this.uploadDocList.length){
            return false;
          }
          let tmp = [];
          for(let i = 0; i < this.uploadDocList.length; i++){
            if(this.uploadDocList[i] !== this.uploadDocList[index]){
              tmp.push(this.uploadDocList[i]);
            }
          }
          this.uploadDocList = tmp;
        }

        if(this.columnName === '音频'){
          if(isNaN(index) || index >= this.uploadAudioList.length){
            return false;
          }
          let tmp = [];
          for(let i = 0; i < this.uploadAudioList.length; i++){
            if(this.uploadAudioList[i] !== this.uploadAudioList[index]){
              tmp.push(this.uploadAudioList[i]);
            }
          }
          this.uploadAudioList = tmp;
        }

        if(this.columnName === '图片'){
          if(isNaN(index) || index >= this.uploadPicList.length){
            return false;
          }
          console.log('uuu')
          let tmp = [];
          for(let i = 0; i < this.uploadPicList.length; i++){
            if(this.uploadPicList[i] !== this.uploadPicList[index]){
              tmp.push(this.uploadPicList[i]);
            }
          }
          this.uploadPicList = tmp;
        }
      },
      doSubmit(){
        let params = {
          classify: this.tagId,    // 针对视频资源时对应的分类id
          file: this.fileIdArr,  // 选择完文件后,调用uploadFile这个接口,后端返回的数组
          resourceColumnId: JSON.parse(localStorage.getItem('columnId')),  // 资源栏目id(视频、图片、音频、文档)
          title: this.uploadTitle  // 上传时填写的标题
        };
        let columnName = localStorage.getItem('columnName')
        this.$api.post(addResources, params).then(res => {
          if(res.data.code === 1001){
            if(columnName === '视频'){
              this.$router.push({name: 'myVideo'});
            }else {
              this.$router.push({name: 'myResourceClassify'});
            }
          }
        }).catch(err => {
          console.log(err)
        })
      },
    },
    mounted(){
      this.getClassifyList();
    }
  }
</script>
<style lang="less" scoped>
  .upload{
    padding: 140px 36px 160px 36px;
    box-sizing: border-box;
  }

  .forPreview_video{
    position: relative;
    /*background: rgba(0,0,0,.7);*/
    video{
      width: 95%;
      max-height: 430px;
    }
    .delte{
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
  .forPreview_doc, .forPreview_audio{
    display: flex;
    margin-bottom: 10px;
    align-items: center;
    img{
      width: 56px;
      height: 56px;
      margin-right: 20px;
    }
    span:nth-of-type(1){
      flex: 1;
    }
    span:nth-of-type(2){
      margin-right: 20px;
    }
  }
  .forPreview_pic{
    display: flex;
    align-items: flex-end;
    img{
      width: 160px;
      height: 160px;
    }
  }

  .diy-detail{
    width: 100%;
    overflow: hidden;
    
    .btn{
      span{
        margin-bottom: 10px;
      }
    }
    .van-cell{
      background-color: #F0F0F0;
      border-radius: 35px;
      font-size: 26px;
      height: 69px;
      line-height: 69px;
      padding: 0 22px;
      color: #999;
    }
    .van-hairline--top-bottom::after, .van-hairline-unset--top-bottom::after {
      border-width: 0;
    }
    p{
      height: 64px;
      line-height: 64px;
      font-size: 32px;
      color: #333;
      position: relative;
      padding-left: 16px;
    }
    p::before{
      position: absolute;
      top: 0;
      left: 0;
      content: '*';
      color: #FF0000;
    }

    span{
      display: inline-block;
      width: 157px;
      background: #F0F0F0;
      border-radius: 35px;
      color: #999;
      font-size: 26px;
      padding: 14px 18px;
      margin-right: 28px;
      text-align: center;
    }
    .active{
      color: #fff;
      background: linear-gradient(to right,#FD5130,#FA6C34);
    }
  }
  .diy-submit {
    position: fixed;
    height: 150px;
    width: 90%;
    bottom: 0;
    background: #fff;

    .van-button {
      width: 100%;
      height: 90px;
      border-radius: 45px;
      font-size: 36px;
      color: #fff;
      background: linear-gradient(to right, #FD5130, #FA6C34);
      top: 50%;
      transform: translate(0, -50%);
    }
    .van-button--default {
      border: none;
    }
  }
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript 实现字符串反转的三种方法
Nov 23 Javascript
addEventListener 的用法示例介绍
May 07 Javascript
js实现遮罩层划出效果是生成div而不是显示
Jul 29 Javascript
Javascript复制实例详解
Jan 28 Javascript
BootStrap中的table实现数据填充与分页应用小结
May 26 Javascript
浅谈JS函数定义方式的区别
Oct 30 Javascript
AngularJS Toaster使用详解
Feb 24 Javascript
解决vue的变量在settimeout内部效果失效的问题
Aug 30 Javascript
vue-cli 引入jQuery,Bootstrap,popper的方法
Sep 03 jQuery
13 个npm 快速开发技巧(推荐)
Jul 04 Javascript
js对象数组和对象的使用实例详解
Aug 27 Javascript
vue 实现购物车总价计算
Nov 06 Javascript
ES6中let、const的区别及变量的解构赋值操作方法实例分析
Oct 15 #Javascript
vue使用@scroll监听滚动事件时,@scroll无效问题的解决方法详解
Oct 15 #Javascript
vue实现树形结构样式和功能的实例代码
Oct 15 #Javascript
谈谈IntersectionObserver懒加载的具体使用
Oct 15 #Javascript
js实现开关灯效果
Mar 30 #Javascript
JS实现灯泡开关特效
Mar 30 #Javascript
浅谈微信小程序列表埋点曝光指南
Oct 15 #Javascript
You might like
深入PHP许愿墙模块功能分析
2013/06/25 PHP
php从数组中随机选择若干不重复元素的方法
2015/03/14 PHP
php反射类ReflectionClass用法分析
2016/05/12 PHP
php仿微信红包分配算法的实现方法
2016/05/13 PHP
PHP GD库相关图像生成和处理函数小结
2016/09/30 PHP
PHP新特性之字节码缓存和内置服务器
2017/08/11 PHP
JS类中定义原型方法的两种实现的区别
2007/03/08 Javascript
Js实现双击鼠标自动滚动屏幕的示例代码
2013/12/14 Javascript
利用js实现禁止复制文本信息
2015/06/03 Javascript
jQuery+css实现炫目的动态块漂移效果
2016/01/28 Javascript
Vue axios 中提交表单数据(含上传文件)
2017/07/06 Javascript
JavaScript代码实现txt文件的上传预览功能
2018/03/27 Javascript
react结合bootstrap实现评论功能
2020/05/30 Javascript
Vue2.x-使用防抖以及节流的示例
2021/03/02 Vue.js
Python实现简单HTML表格解析的方法
2015/06/15 Python
Python的Django框架中设置日期和字段可选的方法
2015/07/17 Python
Python实现string字符串连接的方法总结【8种方式】
2018/07/06 Python
python使用rpc框架gRPC的方法
2018/08/24 Python
Pycharm简单使用教程(入门小结)
2019/07/04 Python
python自定义时钟类、定时任务类
2021/02/22 Python
基于pytorch的保存和加载模型参数的方法
2019/08/17 Python
pandas实现excel中的数据透视表和Vlookup函数功能代码
2020/02/14 Python
django执行数据库查询之后实现返回的结果集转json
2020/03/31 Python
德国购买踏板车网站:Microscooter
2019/10/14 全球购物
N:Philanthropy官网:美国洛杉矶基础款服装
2020/06/09 全球购物
大学生个人自我鉴定
2013/12/03 职场文书
医科大学毕业生自荐信
2014/02/03 职场文书
广告学专业自荐信范文
2014/02/24 职场文书
投资协议书范本
2014/04/21 职场文书
团委书记的竞聘演讲稿
2014/04/24 职场文书
学用政策心得体会
2014/09/10 职场文书
2015年文明创建工作总结
2015/04/30 职场文书
离婚民事起诉状
2015/08/03 职场文书
ThinkPHP5和ThinkPHP6的区别
2021/03/31 PHP
Java中CyclicBarrier和CountDownLatch的用法与区别
2021/08/23 Java/Android
python双向链表实例详解
2022/05/25 Python