vue3文件上传

<el-upload
      ref="uploadDom"
      action=""
      v-model:file-list="fileList"
      :auto-upload="false"
      multiple
      drag
      :on-preview="openFile"
      :on-change="handleChange"
      class="uploadElement"
    >
      <el-icon class="el-icon--upload"><upload-filled /></el-icon>
      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
    </el-upload>
import type { UploadProps, UploadRawFile } from "element-plus";

let fileList = ref([]);

const openFile = (e) => {
  let a = document.createElement("a");
  let event = new MouseEvent("click");
  a.target = "_blank";
  a.download = e.name;
  a.href = e.url;
  a.dispatchEvent(event);
};
const handleChange = (file:any, files:any) => {
  console.log('file',file)
  console.log('files',files)
  fileList.value=files
};
let uploadDom = ref();
const handleExceed: UploadProps["onExceed"] = (files, uploadFiles) => {
  uploadDom.value!.clearFiles();
  const file = files[0] as UploadRawFile;
  file.uid = genFileId();
  uploadDom.value!.handleStart(file);
  fileList.value = uploadFiles;
};

let classicCaseAttachmentFile = ref();
const handleChange = (file, files) => {
  if (files) {
    classicCaseAttachmentFile.value = files;
  }
};

const handleSubmit = async () => {
 // 创建一个FormData实例,用于存储要发送的文件数据  
  let formData = new FormData();
 // 添加文件到FormData实例中  
  formData.append("id", route.query.id as string);

  fileList.value.forEach((item, index) => {
    formData.append(`checkAttachmentFile[${index}]`, item.raw);
  });
  let res: any = await updateCaseInfoAndStartProcess(formData);
  if (res.code == 0) {
    
  }
};



点击按钮下载


const handleDownload = async () => {

  let res: any = await downloadClassicCase();
 // 创建一个Blob对象URL  
  const url = window.URL.createObjectURL(new Blob([res]));
  // 创建一个新的<a>标签  
  const link = document.createElement("a");
  link.style.display = "none";
  link.href = url;
 // 设置下载的文件名(如果服务器没有提供)
  link.setAttribute("download", "aaa.doc");
// 触发点击事件来下载文件  
  document.body.appendChild(link);
  link.click();
// 清理(可选)  
  // 在下载完成后,你可以从DOM中移除这个<a>标签,并释放Blob URL  
  link.remove();
};



export const caseInfoUpdate = (data?: any) => {
  return service({
    url: "/bb/update",
    method: "post",
    data,
    headers: { "Content-Type": "multipart/form-data" },//告知axios发送multipart/form-data类型的数据
  });
};
export const downloadClassicCase = () => {
  return service({
    url: "/cc/Case",
    method: "get",
    responseType: "blob",
  });
};

上传一个
 <el-upload
      ref="uploadDom"
      action=""
      v-model:file-list="fileList"
      :auto-upload="false"
      :limit="1"
      drag
      :on-exceed="handleExceed"
      :on-change="handleChange"
      :on-preview=" openFile"
      class="uploadElement"
    >
      <el-icon class="el-icon--upload"><upload-filled /></el-icon>
      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
    </el-upload>


let classicCaseAttachmentFile = ref();
let fileList = ref([]);
const openFile = (e) => {
  let a = document.createElement("a");
  let event = new MouseEvent("click");
  a.target = "_blank";
  a.download = e.name;
  a.href = e.url;
  a.dispatchEvent(event);
};
let uploadDom = ref();
const handleExceed: UploadProps["onExceed"] = (files, uploadFiles) => {
  uploadDom.value!.clearFiles();
  const file = files[0] as UploadRawFile;
  file.uid = genFileId();
  uploadDom.value!.handleStart(file);
  fileList.value = uploadFiles;
};
const handleChange = (file, files) => {
  if (files) {
    classicCaseAttachmentFile.value = files;
  }
};

const handleDownload = async () => {
  let res: any = await downloadClassicCase();
  const url = window.URL.createObjectURL(new Blob([res]));
  const link = document.createElement("a");
  link.style.display = "none";
  link.href = url;
  link.setAttribute("download", "哈哈哈.doc");
  document.body.appendChild(link);
  link.click();
  link.remove();
};
const handleSubmit = async () => {

    // 新增/编辑
    let formData = new FormData();
    formData.append("id", route.query.id as string);
    formData.append(
      "classicCaseAttachmentFile",
      classicCaseAttachmentFile.value[0].raw
    );
    let res: any = await caseInfoUpdate(formData);
    if (res.code == 0) {
  
    }
  }
};

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Vue3文件上传删除功能,你可以使用以下步骤: 1. 创建一个文件上传组件,在该组件中添加上传文件的功能。 2. 在文件上传组件中,添加一个删除文件的按钮。 3. 在点击删除按钮时,将该文件的文件名传递给一个删除文件的方法。 4. 在删除文件的方法中,使用axios或者其他的网络请求库,向后端发送一个删除该文件的请求。 5. 当后端成功删除该文件时,将该文件从文件列表中删除。 下面是一个简单的Vue3文件上传删除功能的代码示例: ```vue <template> <div> <input type="file" @change="onFileChange"> <button @click="deleteFile">删除文件</button> <ul> <li v-for="(file, index) in fileList" :key="index">{{file.name}}</li> </ul> </div> </template> <script> import { reactive } from 'vue' import axios from 'axios' export default { setup() { const state = reactive({ fileList: [] }) const onFileChange = (event) => { const files = event.target.files for (let i = 0; i < files.length; i++) { state.fileList.push(files[i]) } } const deleteFile = () => { const fileName = state.fileList[0].name // 假设只有一个文件 axios.delete(`/api/file/${fileName}`).then(() => { state.fileList.splice(0, 1) }) } return { state, onFileChange, deleteFile } } } ``` 在这个示例代码中,我们假设后端提供了一个删除文件的API,该API的URL为`/api/file/${fileName}`,其中`fileName`是要删除的文件的文件名。在点击删除按钮时,我们发送一个DELETE请求到该API,请求成功后,将该文件从文件列表中删除。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值