uploadFile.vue 3.27 KB
Newer Older
1
<template>
qzhxx's avatar
qzhxx committed
2
<div>
qzhxx's avatar
qzhxx committed
3

4
  <el-upload
qzhxx's avatar
qzhxx committed
5
    ref="upload"
liqin's avatar
liqin committed
6
    accept=".mp4,.flv,.mpeg,.mpg,.mov"
qzhxx's avatar
qzhxx committed
7 8 9 10 11 12 13 14
    :multiple="true"
    :http-request="uploadFile"
    :file-list="fileList"
    action
    :auto-upload="false"
    :before-upload="beforeAvatarUpload"
  >
    <i class="el-icon-plus fileUpload"></i>
liqin's avatar
liqin committed
15

qzhxx's avatar
qzhxx committed
16
  </el-upload>
17
   <el-button style="margin-left: 10px;" size="small" type="success" v-loading="loading" @click="submitUpload">上传到服务器</el-button>
qzhxx's avatar
qzhxx committed
18
</div>
liqin's avatar
liqin committed
19

20 21 22
</template>

<script>
qzhxx's avatar
qzhxx committed
23
export default {
qzhxx's avatar
qzhxx committed
24 25 26 27 28 29
   props:{
    fileList:{
      type:Array,
      default:[]
    },
  },
qzhxx's avatar
qzhxx committed
30 31
  data() {
    return {
qzhxx's avatar
qzhxx committed
32
      fullscreenLoading: false,
33
      loading:false,
qzhxx's avatar
qzhxx committed
34 35 36 37 38 39 40
      filedata:[],
      fileArr: [],
      fileList:[],
    //   fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
    };
  },
  methods: {
qzhxx's avatar
qzhxx committed
41 42 43 44 45 46 47
     openFullScreen2() {
        const loading = this.$loading({
          lock: true,
          text: '上传中,请稍候。。。',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
liqin's avatar
liqin committed
48

qzhxx's avatar
qzhxx committed
49
      },
qzhxx's avatar
qzhxx committed
50
    submitUpload() {  // 导入
qzhxx's avatar
qzhxx committed
51 52 53 54 55 56 57 58
    // this.openFullScreen2()
     const loading = this.$loading({
          lock: true,
          text: '上传中,请稍候。。。',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
    // this.loading = true
qzhxx's avatar
qzhxx committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        let tempData =  this.filedata
        this.filedata = new FormData()  //  用FormData存放上传文件
        this.$refs.upload.submit()  // 会循环调用uploadFile方法,多个文件调用多次

         let _this = this;
      _this
        .$https(
          {
            method: "post",
            url: "file/video/content/upload",
            authType: this.backToken
          },
          this.filedata
        )
        .then(res => {
          let resData = res.data;
          console.log(res)
qzhxx's avatar
qzhxx committed
76
          loading.close();
qzhxx's avatar
qzhxx committed
77
          // this.loading = false
qzhxx's avatar
qzhxx committed
78
          if (resData.resultCode == "200") {
liqin's avatar
liqin committed
79

80
             _this.$message.success('上传成功!')
qzhxx's avatar
qzhxx committed
81 82 83 84 85 86 87
            const data = resData.data.fileList;
            let newArray = data.map((item) => item.id);
            let editArray =[]
            if(this.fileList.length){
              editArray = this.fileList.map(item=>item.id)
            }
            this.$emit("videoList", [...newArray,...editArray]);
liqin's avatar
liqin committed
88

qzhxx's avatar
qzhxx committed
89 90 91 92 93 94 95 96 97
          } else {
            _this.$message.error(resData.msg || resData.message);
          }
        })
        .catch(err => {
          console.log(err);
          _this.$message.error(err.msg || err.message);
        });

liqin's avatar
liqin committed
98

99
    },
qzhxx's avatar
qzhxx committed
100
    uploadFile(file) {
qzhxx's avatar
qzhxx committed
101
         console.log(file)
qzhxx's avatar
qzhxx committed
102
         this.filedata.append('file', file.file)
103
    },
qzhxx's avatar
qzhxx committed
104
    beforeAvatarUpload(file) {
liqin's avatar
liqin committed
105
      const isLt10G = file.size / 1024 / 1024 / 1024 < 10;
liqin's avatar
liqin committed
106 107
      if (!isLt10G) {
        this.$message.error("上传文件大小不能超过 10GB!");
108
      }
liqin's avatar
liqin committed
109
      return isLt10G;
110 111
    }
  }
qzhxx's avatar
qzhxx committed
112
};
113 114 115
</script>

<style>
116
.el-upload .fileUpload {
qzhxx's avatar
qzhxx committed
117 118
  width: 148px;
  height: 148px;
119
  line-height: 148px!important;
qzhxx's avatar
qzhxx committed
120 121 122 123 124
  margin: 0 auto;
  border: 1px dashed #c0ccda;
  font-size: 24px;
  font: #ccc;
}
liqin's avatar
liqin committed
125
</style>