uploadFolder.vue 3.17 KB
Newer Older
qzhxx's avatar
qzhxx committed
1
<template>
qzhxx's avatar
qzhxx committed
2 3 4 5 6
  <el-form enctype="multipart/form-data">
    选择多文件:
    <input type="file" name="files" multiple="multiple" @change="getFiles($event)" />
    <br />选择文件夹:
    <input @change="getFiles($event)" type="file" name="files" webkitdirectory mozdirectory />
qzhxx's avatar
qzhxx committed
7
    <br />
qzhxx's avatar
qzhxx committed
8 9 10 11 12 13 14 15 16 17
    <ul class="el-upload-list el-upload-list--text">
      <li class="el-upload-list__item is-ready" v-for="(item, i) in files" :label="item.name" :value="item.name" :key="i">
        {{item.name}}
        <span>
          <el-button size="mini" type="text">
            <i @click="remove(item)" class="el-icon-close"></i>
          </el-button>
        </span>
      </li>
    </ul>
qzhxx's avatar
qzhxx committed
18

qzhxx's avatar
qzhxx committed
19 20
    <el-button size="medium" type="success" @click.stop="upload">上传到服务器</el-button>
  </el-form>
qzhxx's avatar
qzhxx committed
21 22 23 24 25 26
</template>

<script>
import axios from "axios";
export default {
  name: "UploadFileVue",
qzhxx's avatar
qzhxx committed
27 28 29 30 31 32
   props:{
    fileList:{
      type:Array,
      default:[]
    },
  },
qzhxx's avatar
qzhxx committed
33 34
  data() {
    return {
qzhxx's avatar
qzhxx committed
35 36 37 38
      fullscreenLoading: false,
      files: [
        
      ]
qzhxx's avatar
qzhxx committed
39 40
    };
  },
qzhxx's avatar
qzhxx committed
41 42 43 44 45 46 47
  watch: {
    fileList(newName, oldName) {
      console.log(newName)
      this.files = newName
      // ...
    }
  },
qzhxx's avatar
qzhxx committed
48 49 50 51 52 53 54
  computed: {
    headers() {
      return {
        Authorization: localStorage.getItem("backToken") || ""
      };
    }
  },
qzhxx's avatar
qzhxx committed
55
  
qzhxx's avatar
qzhxx committed
56
  methods: {
qzhxx's avatar
qzhxx committed
57 58 59 60
    remove(data) {
      const children = this.files;
      const index = children.findIndex(d => d.name === data.name);
      children.splice(index, 1);
qzhxx's avatar
qzhxx committed
61 62
      console.log(this.files,'111')
      this.$emit("videoList", this.files.map(item=>item.id));
qzhxx's avatar
qzhxx committed
63
    },
qzhxx's avatar
qzhxx committed
64
    getFiles: function(event) {
qzhxx's avatar
qzhxx committed
65
      console.log(event);
qzhxx's avatar
qzhxx committed
66 67 68 69
      var files = event.target.files;
      for (var i = 0; i < files.length; i++) {
        this.files.push(files[i]);
      }
qzhxx's avatar
qzhxx committed
70
      console.log(this.files, "--");
qzhxx's avatar
qzhxx committed
71 72
    },
    upload: function() {
qzhxx's avatar
qzhxx committed
73 74 75 76 77 78
        const loading = this.$loading({
          lock: true,
          text: '上传中,请稍候。。。',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
qzhxx's avatar
qzhxx committed
79 80 81 82 83
      console.log(this.files);
      var formData = new FormData();
      for (var i = 0; i < this.files.length; i++) {
        formData.append("file", this.files[i]);
      }
qzhxx's avatar
qzhxx committed
84
      console.log(formData);
qzhxx's avatar
qzhxx committed
85 86 87 88 89 90 91 92 93 94 95
      let _this = this;
      _this
        .$https(
          {
            method: "post",
            url: "file/video/content/upload",
            authType: this.backToken
          },
          formData
        )
        .then(res => {
qzhxx's avatar
qzhxx committed
96
           loading.close();
qzhxx's avatar
qzhxx committed
97 98 99 100
          let resData = res.data;
          console.log(res);
          if (resData.resultCode == "200") {
            _this.$message.success("上传成功!");
qzhxx's avatar
qzhxx committed
101 102 103 104 105 106 107
            const data = resData.data.fileList;
            let newArray = data.map((item) => item.id);
            let editArray =[]
            if(this.files.length){
              editArray = this.files.map(item=>item.id)
            }
            this.$emit("videoList", [...newArray,...editArray]);
qzhxx's avatar
qzhxx committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
          } else {
            _this.$message.error(resData.msg || resData.message);
          }
        })
        .catch(err => {
          console.log(err);
          _this.$message.error(err.msg || err.message);
        });
    }
  }
};
</script>

<style>
</style>