• vue+element 递归上传图片


    直接上代码。
    <template>
      <div>
        <el-upload
          action="http://localhost:3000/picture"
          :http-request = "getimages"
          :before-upload = "beforeUp"
          :headers="headers"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-progress="progress"
          :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
        
        <el-row>
          <el-button type="info" plain @click="upload">信息按钮</el-button>
        </el-row>
     </div>
    </template>
    <script>
      export default {
        data() {
          return {
            dialogImageUrl: '',
            dialogVisible: false,
            headers:{},
            imgArr:[],
            index:0,
            formData:new FormData()
          };
        },
        methods: {
          beforeUp(file){
            // console.log(file);
            /* 获取图片原本的二进制对象,并存储到图片数组模型中 */
            this.imgArr.push(file)
          },
          /* 移除 */
          handleRemove(file, fileList) {
            // console.log(file, fileList);
            console.log('移除时获取的图片的 uid = '+file.uid);
            this.imgArr =  this.imgArr.filter(t=>t.uid!=file.uid)
          },
          /* 预览 */
          handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
            // console.log(file);
          },
          /* 上传中 */
          progress(){
            console.log('上传中');
          },
          /* 代替默认上传图片方法 */
          getimages(res){
            
          },
          /* 最后点击上传 */
          upload(){
              this.a1()
          },
          /* 递归1条条上传 */
          a1(){
            if(this.index<this.imgArr.length){
              if(this.index==this.imgArr.length){
                return
              }
              this.formData.delete('file')
              this.a2()
            }
          },
          async a2(){
            console.log(this.index);
            this.formData.append('file',this.imgArr[this.index]);
            this.$http.post('/picture',this.formData)
            this.index++
            this.a1()
          }
        },
        created(){
          // this.$http.get('/picture')
          // this.headers ={Authorization : 'Bearer '+(localStorage.token || '')}
        }
      }
    </script>
    存在疑惑的地方可以留言一起讨论 。
  • 相关阅读:
    实现免费WiFi无线共享
    详解spring配置文件
    P1886 滑动窗口 /【模板】单调队列
    P3370 【模板】字符串哈希
    P3371 【模板】单源最短路径(弱化版)
    P3367 【模板】并查集
    P1177 【模板】快速排序
    P3382 【模板】三分法
    P3374 【模板】树状数组 1
    P1226 【模板】快速幂||取余运算
  • 原文地址:https://www.cnblogs.com/500m/p/11914764.html
Copyright © 2020-2023  润新知