vue3+ts+elementPlus上传一个文件替换之前得

 <el-upload

       ref="uploadRef"

      :limit="1"

       :on-exceed="handleExceed"

      accept=".doc,.docx,.pdf"

     :action="upload.url"

     :disabled="upload.isUploading"

     :on-progress="handleFileUploadProgress"

     :on-success="handleFileSuccess"

      :show-file-list="true"

      :auto-upload="true"

       :file-list="fileList"

         multiple

 >

<el-button type="primary">上传</el-button>

<template #tip>

<div class="el-upload__tip">

      <span>仅允许导入word、pdf格式文件。</span>

      <!-- <el-link

      type="primary"

        :underline="false"

       style="font-size: 12px; vertical-align: baseline"

     @click="importTemplate"

   >下载模板</el-link

    >   -->

       </div>

  </template>

</el-upload>

ts

<script setup name="cpglnew">

import { reactive, ref } from 'vue'

const { proxy } = getCurrentInstance()

/*** 导入参数 */

const upload = reactive({

    // 是否显示弹出层(导入)

    open: false,

    // 弹出层标题(导入)

    title: '',

    // 是否禁用上传

    isUploading: false,

    // 是否更新已经存在的用户数据

    // updateSupport: 0,

    // 设置上传的请求头部

    // headers: { Authorization: 'Bearer ' + getToken() },

    // 上传的地址

    url: import.meta.env.VITE_APP_BASE_API + '/common/upload' // 上传文件服务器地址

})

const fileList=ref([])

/** 修改按钮操作 */

function handleUpdateforecast(row) {

    fileList.value=[]

    getList()

    resetforecast()

    // console.log('row',row)

    // Object.keys(forecastform.value).forEach((key) => {

    //     forecastform.value[key] = row[key]

    // })

   

    const forecastId = row.forecastId

    getForecastListinfo(forecastId).then((response) => {

    forecastform.value = response.data

    console.log('forecastform.value',forecastform.value.filePath)

//文件名显示,替换上个文件

    let file={}

    file.name=forecastform.value.fileName

    file.url=forecastform.value.filePath

    if(forecastform.value!==''){

        fileList.value.push(file)

    }

    // open.value = true

    forecastitle.value = '修改'

  })

//替换文件

const handleExceed = (file, fileList) => {

    // console.log('file',file)

    if(file){

         proxy.$refs['uploadRef'].clearFiles()

    }

    proxy.$refs['uploadRef'].handleStart(file[0])

  // 替换掉fileList中的第一个文件

//   proxy.$refs["uploadRef"].handleRemove(file);

    //  fileList.value = [file];

     proxy.$refs["uploadRef"].submit();

};

/** 下载模板操作 没做*/

function importTemplate() {

  proxy.download("/common/download", {

  }, `ceshi_template_${new Date().getTime()}.xlsx`);

};

/**文件上传中处理 */

const handleFileUploadProgress = (event, file, fileList) => {

  upload.isUploading = true;

  console.log('event',event)

  console.log('file',file)

};

/** 文件上传成功处理 */

const handleFileSuccess = (response, file, fileList) => {

    upload.open = false;

    upload.isUploading = false;

    forecastform.value.filePath = response.url

    // proxy.$refs["uploadRef"].handleRemove(file);

    proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });

    getList();

};

/** 提交上传文件 */

function submitFileForm() {

  proxy.$refs["uploadRef"].submit();

};

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值