vue 檔案上傳方法formData

周家大小姐.發表於2020-10-13

html:

   <el-upload
              ref="upload"
              drag
              action=""
              class="uploader"
              accept=".xls,.xlsx"
              :multiple="false"
              :on-change="handleChange"
              :on-remove="handleRemove"
              :file-list="fileList"
              :auto-upload="false"
            >
              <i class="el-icon-upload"/>
              <div class="el-upload__text">將檔案拖到此處,或<em>點選上傳</em></div>
              <div slot="tip" class="sale-tips-text">只能上傳xls檔案,請下載模板填寫</div>
            </el-upload>
          </div>
        </div>
      </div>
      <!-- button -->
      <div class="sale-m-t-16 text-center">
        <el-button class="sale-button-default" type="primary" round @click="handleUpload">匯入</el-button>

formData方法

 // 匯入客戶介面
      uploadCust() {
        this.loading = true
        const formData = new FormData()
        formData.append('upload', this.fileList[0].raw)
        const params = this.uploadCustomerParams
        for (const index in params) {
          formData.append(index, params[index])
        }
        uploadCust(formData)
          .then(resp => {
            // this.$message.success('匯入成功')
         
          })
          .catch(e => {
            this.$message.error(e.toString())
            this.loading = false
          })
      },

 

相關文章