用vue+el-upload+new FormData()实现多图片上传

用vue+el-upload+new FormData()实现多图片上传
如图
在这里插入图片描述

   <div class="relicsImage_content">
      <el-upload
        class="upload_reilcimg"
        ref="uploadImg"
        :action="baseurl + '/antique/batch_img'"
        list-type="picture-card"
        :headers="headers"
        :before-upload="beforeAvatarUploadMore"
        :on-success="handleAvatarSuccess"
        :on-remove="handleRemove"
        :on-change="changeUpload"
        :show-file-list="true"
        accept="image/png, image/jpeg, application/pdf"
        :multiple="true"
        :auto-upload="false"
      >
        <i class="el-icon-plus" />
      </el-upload>
      <p style="font-size: 12px; color: #ccc; margin-top: 20px">
        可上传pdf、jpg、jpeg、png格式,不能超过50M
      </p>
    </div>
    <div>
      <el-button type="primary" @click="uploadSumit()">确定</el-button>
      <el-button @click="reset()">重置</el-button>
    </div>
  data() {
    return {
      ssd: [],
      isActive: false,
      forma: {
        copyReportCode: "",
      },
      list: [],
      page: 1,
      limit: 5, //
      total: 0,
      id: "", //代表选中某一行的id
      multipleSelection: [],
      uploadMoreimg: {
        fileList: [],
      },
      url: this.baseurl + "/antique_levy/upload",
      headers: {
        Authorization: sessionStorage.getItem("token"),
      },
    };
  },
  created() {},
  methods: {
    reset() {
      this.$refs.uploadImg.clearFiles();
    },
    uploadSumit() {
      // 创建新的数据对象
      let formData = new FormData();
      // 将上传的文件放到数据对象中
      this.uploadMoreimg.fileList.forEach((file) => {
        formData.append("file", file.raw);
      });
      console.log("提交前", formData.getAll("file"));
      // 自定义上传
      this.axios
        .post("/antique/batch_img", formData)
        .then((response) => {
          console.log(response);
          this.isshowMoreimg = false;
          // if(response.code == 200){
          //   this.$refs.uploadImg.clearFiles();
          //   this.msgSuccess('上传成功!');
          // }else{
          //   this.$message.error(response.msg);
          // }
        })
        .catch((error) => {
          this.$message.error("上传失败!");
        });
    },
    openDialog(employeeId) {
      this.isshowMoreimg = true;
    },
    downLoad(row) {
      this.downloadByA({
        url: this.baseurl + "/collection_copy_report/print?id=" + row.id,
        name: "藏品编目",
      });
    },
    downloadByA({ url, name = "temp" }) {
      // 生成一个a元素
      const a = document.createElement("a");
      // 创建一个单击事件
      const event = new MouseEvent("click");
      // 设置图片名称
      a.download = name;
      // 如果跳转页面,则在其他页面跳转
      a.target = "_blank";
      // 将生成的URL设置为a.href属性
      a.href = url;
      // 触发a的单击事件
      a.dispatchEvent(event);
    },

    // 上传之前
    beforeAvatarUploadMore(file) {
      const isJPG = file.type === "image/jpeg";
      const isPng = file.type === "image/png";
      const isPdf = file.type === "application/pdf";
      //   const isLt50M = file.size / 1024 / 1024 < 50;
      //   if (!isLt50M) {
      //     this.$message.warning("上传文件大小不能超过 50MB!");
      //     return false;
      //   }
      //   if (!isJPG && !isPng && !isPdf) {
      //     this.$message.warning("只能上传 JPG/PNG/PDF/JPEG 格式!");
      //     return false;
      //   }
    },
    // 上传成功
    handleAvatarSuccess() {},
    // 删除
    handleRemove(file, fileList) {
      this.uploadMoreimg.fileList = fileList;
    },
    // 文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用
    changeUpload(file, fileList) {
      this.uploadMoreimg.fileList = fileList;
    },
  },
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,关于多个文件上传和提交同时进行的控制,可以使用 layui.upload 的 before 函数来实现。 示例代码如下: ``` layui.use('upload', function(){ var upload = layui.upload; //多文件列表示例 var demoListView = $('#demoList'), uploadListIns = upload.render({ elem: '#testList', url: '/upload/', accept: 'file', multiple: true, auto: false, bindAction: '#testListAction', choose: function(obj){ var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列 //读取本地文件 obj.preview(function(index, file, result){ var tr = $(['<tr id="upload-'+ index +'">', '<td>'+ file.name +'</td>', '<td>'+ (file.size/1014).toFixed(1) +'kb</td>', '<td>等待上传</td>', '<td>', '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>', '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>', '</td>', '</tr>'].join('')); //单个重传 tr.find('.demo-reload').on('click', function(){ obj.upload(index, file); }); //删除 tr.find('.demo-delete').on('click', function(){ delete files[index]; //删除对应的文件 tr.remove(); uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选 }); demoListView.append(tr); }); }, before: function(obj){ layer.load(); //上传loading //模拟loading setTimeout(function(){ layer.closeAll('loading'); }, 2000); }, done: function(res, index, upload){ if(res.code == 0){ //上传成功 var tr = demoListView.find('tr#upload-'+ index), tds = tr.children(); tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>'); tds.eq(3).html(''); //清空操作 return delete this.files[index]; //删除文件队列已经上传成功的文件 } this.error(index, upload); }, error: function(index, upload){ var tr = demoListView.find('tr#upload-'+ index), tds = tr.children(); tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>'); tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传 } }); }); ``` 其中,before 函数用于上传之前的处理,这里可以添加 loading 等提示信息,确保上传过程中用户可以看到正在处理。 另外,由于 layui.upload 是基于 layui.form 实现的,所以在页面中需要有一个 form 标签,同时需要设置一个 bindAction 参数指向提交按钮,这样在上传完成后才能触发表单提交。 希望这个示例代码能够帮助到您。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值