el-upload 上传多张图片,采用formData方式上传

            <el-upload
              ref="upload"
              action="#"//上传的服务器地址
              :auto-upload="false"//是否自动上传,false不自动上传
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"//图片预览
              :on-remove="handleRemove"//删除图片方法
              :on-change="(file, fileList) => {uploadSuccess(file, fileList)}" //上传成功或者失败都会调用on-change
              :file-list="imageList" //回显的列表
              >
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible" append-to-body class="checkPic">
              <img width="100%" :src="dialogImageUrl" alt="">//图片预览的弹框
            </el-dialog>
export dafault {
  data (){
    return {
        dialogImageUrl: '',
        imageList:[],
        fileList:[],
      }
  },
methods:{
      //删除已上传图片
      handleRemove(file, fileList) {
        //file是当前删除的图片,fileList是已上传图片列表
          this.imageList =fileList
          this.fileList = fileList
        },
      //图片预览
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
        },
      // 上传成功时的钩子
      uploadSuccess(file,fileList){
        console.log(fileList)
        let that =this
        var fileType = file.raw.type;
        var isJpg = false;
        if (fileType == 'image/jpeg' || fileType == 'image/png' || fileType == 'image/jpg') {
            isJpg = true;
        }           
        if (!isJpg) {
            this.$message({
            message: '上传的图标只能是jpg、png格式!',
            type: 'error'
          });
          return false
        }
         this.fileList= fileList
        this.imageList =fileList
      },
        let formList = new FormData();
        this.fileList.forEach( (file)=>{
          formList.append('fileList', file.raw);
        })
      //formList就是传给后台的值啦
  }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容