vue解决upload组件第二次上传无反应

5 篇文章 0 订阅

问题描述:

附件上传组件名: Accessory

组件内容: 为方便更多页面调用该组件进行附件上传。

问题详述: 父级页面为编辑页面调用该组件,在点击编辑按钮后上传附件,第一次上传成功,提交后在其他行再次点击编辑按钮,调用该组件,则显示上传失败。

解决关键:

  • 第一次附件上传后el-upload中的file没有清空。
  • 我们在附件的组件中为el-upload添加ref属性,并增加方法clearFiles,代码如下:
 clearFiles(){
      this.$refs.upload.clearFiles();
    }
  • 在父级页面,调用该组件:关键点: ref="Accessory"@clearFiles="clearFiles"
 <Accessory
            ref="Accessory"
            :dataForm="dataForm"
            :Attachment="Attachment"
            @clearFiles="clearFiles"
            @DeleteAttachment="move_secerurl_filename_update"
            @beforeUpload="beforeAvatarUpload_secerurl_update"
            @DownloadAttachment="download_secerurl(edit_certificateForm.url)"
            @handleSuccess="handleSuccess_secerurl_edit"
          >
          </Accessory>
  • 编辑该方法
 clearFiles(){
      this.$refs.Accessory.clearFiles()
    },
  • 在附件上传完成后或编辑提交成功后调用该方法:this.clearFiles();
 // 更新证书
    updateCertification() {
      this.edit_certificateForm.welderid = this.dataForm.id;
      this.edit_certificateForm.welder = this.dataForm.weldername;
      this.$refs["edit_certificateFormRef"].validate(valid => {
        if (valid) {
          this.$api.welderQualification
            .save(this.edit_certificateForm)
            .then(res => {
              if (res.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.edit_dialogVisible = false;
                this.doRefreshCertifications(
                  this.edit_certificateForm.welderid
                );
                this.Attachment="";
                this.clearFiles();
              } else {
                this.$message({
                  message: "操作失败, " + res.msg,
                  type: "error"
                });
              }
            });
        }
      });
    },

组件代码:

<template>
  <div>
    <strong v-if="show_maintain">
      <p style="color: #3d7878; font-size: 16px; margin: 20px">
        <strong style="font-size: 20px">|</strong>
        &nbsp;&nbsp;附件维护
      </p>
    </strong>
    <strong v-if="!show_maintain">
      <p style="color: #3d7878; font-size: 16px; margin: 20px">
        <strong style="font-size: 20px">|</strong>
        &nbsp;&nbsp;证书附件
      </p>
    </strong>
    <table
      width="90%"
      border="1"
      bordercolor="#E4E7ED"
      cellpadding="10px"
      cellspacing="0"
      style="margin-left: 8%"
    >
      <tr height="35px">
        <th>附件名</th>
        <th>资料名</th>
        <th>操作</th>
      </tr>
      <tr height="50px">
        <td width="10%" align="center">附件</td>
        <td width="42%" align="center">
          <el-tooltip
            class="item"
            effect="light"
            content="点击下载"
            placement="bottom"
          >
            <el-link
              type="primary"
              @click="DownloadAttachment"
              v-if="Attachment"
              >资料附件
            </el-link>
          </el-tooltip>
        </td>
        <td width="26%" align="center">
          <el-upload
            ref="upload"
            style="float: left; margin-left: 10%"
            align="center"
            :limit="1"
            :action="baseUrl + '/upLoad/annex'"
            :show-file-list="false"
            :before-upload="beforeUpload"
            :on-success="handleSuccess"
          >
            <el-button plain size="mini">上传</el-button>
          </el-upload>
          <el-button
            style="float: left; margin-left: 10%"
            plain
            type="danger"
            size="mini"
            @click="DeleteAttachment"
          >
            删除
          </el-button>
        </td>
      </tr>
    </table>
  </div>
</template>

<script>
import { baseUrl } from '@/utils/global.js'
import { imageRemoteUrl } from '@/utils/global.js'
export default {
  name: "Accessory",
  components: {},
  props: {
    dataForm: {
      type: Object
    },
    Attachment: {
      type: String
    },
    //显示维护界面时,描述是“附件维护”,在证书新增编辑界面,显示“证书附件”
    show_maintain: {
      type: Boolean,
      default: true
    },
  },
  data() {
    return {
      baseUrl: baseUrl,
      imageRemoteUrl: imageRemoteUrl,
    };
  },
  created() { },
  methods: {
    handleSuccess(res) {
      this.$emit("handleSuccess", res);
    },
    handlePreview(file) {
      this.$emit("handlePreview", file);
    },
    beforeUpload(file) {
      this.$emit("beforeUpload", file);
    },
    DeleteAttachment() {
      this.$emit("DeleteAttachment");
    },
    DownloadAttachment(file) {
      this.$emit("DownloadAttachment", file);
    },
    clearFiles(){
      this.$refs.upload.clearFiles();
    }
  },
  watch: {
    Attachment() { }
  }
};
</script>
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,针对您的问题,我可以提供以下代码示例,用于实现Vue Upload组件上传单个图片并带删除和放大功能: HTML模板代码: ``` <template> <div> <upload accept="image/*" :before-upload="beforeUpload" :on-success="onSuccess" :on-error="onError" > <div class="upload-container"> <div v-if="!imageUrl" class="upload-plus"> <i class="el-icon-plus"></i> <div class="upload-text">上传图片</div> </div> <div v-else class="upload-success"> <img :src="imageUrl" alt="uploaded image"> <div class="upload-actions"> <span class="upload-delete" @click="deleteImage">删除图片</span> <span class="upload-enlarge" @click="enlargeImage">放大图片</span> </div> </div> </div> </upload> </div> </template> ``` Vue组件代码: ``` <script> export default { data() { return { imageUrl: '' } }, methods: { beforeUpload(file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' if (!isJPG) { this.$message.error('上传图片只能是 JPG 或 PNG 格式!') return false } const isLt2M = file.size / 1024 / 1024 < 2 if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB!') return false } }, onSuccess(response) { this.imageUrl = response.data.url }, onError(error) { this.$message.error('上传图片失败!') }, deleteImage() { this.imageUrl = '' }, enlargeImage() { this.$alert(`<img src="${this.imageUrl}" style="max-width: 100%;">`, '放大图片', { dangerouslyUseHTMLString: true }) } } } </script> ``` 在该示例中,我们使用了Vue Upload组件来实现图片上传功能。我们通过`before-upload`钩子函数来限制上传图片的格式和大小。在上传成功时,我们将返回的图片URL赋值给`imageUrl`属性,以便在页面上显示上传的图片。我们还使用了一个删除按钮和一个放大按钮,分别用于删除和放大图片。当用户击放大按钮时,我们使用`$alert`方法来展示放大后的图片。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值