element-ui table 跟 upload 結合 使用 每行 都有一個上傳, 只能上傳一次 問題解決, 以及 如何 在beforeupload 之前 添加 參數


主要是因為 每一行 都有一個上傳,所以需要 設置不一樣的 ref 用來 清空 上傳 的 緩存文件
不論上傳成功還是失敗 清空 當前行的 文件內容   this.$refs['upload' + this.rowsIndex].clearFiles()
<el-upload
            :key="scope.$index"
            :ref="'upload'+ scope.$index"
            action=""
            accept=".doc,.docx"
            :http-request="uploadFileFn"
            :show-file-list="false"
            :limit="1"
            style="display:inline-block"
            :before-upload="(file) => {beforeUpload(file,scope)}"
          >
         <el-button>上傳</el-button>
</el-upload>
 
 // 上傳前
    beforeUpload(file, row) {
      this.isSuffix = false
      this.rowsIndex = row.$index
      // 判斷是否是可上傳的類型
      if (this.strfiletypeextensionCur.filter(val => {
        return getFileExtendingName(file.name).toLowerCase() === val
      }).length === 0) {
        this.isSuffix = true
      }
      if (this.isSuffix) {
        this.$message.error('此文件類型不支持')
        return false
      }
    },
// 上傳
 async uploadFileFn(param) {
      if (param.file.size > 5242880) {
        this.$message.error('請上傳小於5M的附件')
        return false
      }
      if (this.isSuffix === true) {
        this.$refs['upload' + this.rowsIndex].clearFiles()
        return false
      }
      const file = param.file
      const formData = new FormData()
      formData.append('files', file)
      formData.append('presetfolder', JSON.stringify(this.filePath))
      this.loading = true
      const res = await uploadFile(formData)
      this.loading = false
      if (res.code === 20000) {
        this.$message.success(res.message)
      }
      this.$refs['upload' + this.rowsIndex].clearFiles()
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM