vue3+ts实现文件上传

vue3+ts上传文件

<template>
  <div>
    <input type="file" ref="fileInput" @change="handleFileUpload">
    <button @click="uploadFile">Upload</button>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import axios from 'axios';

const fileInput = ref<HTMLInputElement | null>(null);
let selectedFile: File | null = null;

const handleFileUpload = () => {
  if (fileInput.value && fileInput.value.files) {
    selectedFile = fileInput.value.files[0];
  }
};

const uploadFile = async () => {
  if (!selectedFile) {
    console.error("No file selected.");
    return;
  }

  const formData = new FormData();
  formData.append('file', selectedFile);

  try {
    const response = await axios.post('/upload/file', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
    console.log('File uploaded successfully:', response.data);
  } catch (error) {
    console.error('Error uploading file:', error);
  }
};
</script>
  • <input type="file"> 用于选择文件。
  • handleFileUpload 方法用于监听文件选择事件,并将选定的文件存储在 selectedFile 中。
  • uploadFile 方法用于实际的文件上传操作。它创建一个 FormData 对象,将选定的文件附加到其中,然后使用axios来将文件上传到后端。
  • 在axios的请求配置中,设置了请求头为 “multipart/form-data”,以确保正确地处理文件上传。
  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3中使用TypeScript进行多文件上传,你可以按照以下步骤进行操作: 1. 首先,创建一个文件上传组件。你可以使用`custom-upload`组件,并通过`v-model`绑定一个表单项,用于存储上传的文件列表。在`<script>`标签中,引入需要的接口类型和API封装方法。 ```javascript <template> <custom-upload v-model="formInline.attachmentResultList" :attr="attrResult" /> </template> <script> import { ref } from 'vue'; // 引入Vue3的ref方法 import { uploadFile } from '@/api'; export default { setup() { const formInline = ref({ attachmentResultList: [] }); const attrResult = ref({ id: 'fileUploadId', name: 'attachmentName', limit: 999, readonly: true }); return { formInline, attrResult }; } }; </script> ``` 2. 在API文件中,定义多文件上传的方法`uploadFile`。该方法接收一个`FormData`类型的参数,用于包含上传的文件数据。通过调用`http.upload`方法,并指定上传文件的URL路径和请求头,实现文件上传功能。 ```javascript // api.ts import http from '@/api'; import { UPLOAD_FILE } from '@/api/config/servicePort'; export const uploadFile = (params: FormData) => { return http.upload<Upload.ResFileList>(UPLOAD_FILE + '/file/uploadFile', params, { headers: { 'Content-Type': 'multipart/form-data' } }); }; ``` 3. 定义文件上传的接口类型。根据你的需求,可以根据接口返回的数据定义相应的类型。 ```javascript // interface.ts export namespace Upload { export interface ResFileList { id: string; fileName: string; fileUrl: string; uploadTime: string; operator: string; fileType: string; fileSize: number; fileOldname: string; } } ``` 通过上述步骤,你就可以在Vue3中使用TypeScript实现文件上传的功能了。在你的组件中,用户可以选择多个文件进行上传,并且上传成功后,文件相关的信息将会被保存在`formInline.attachmentResultList`中,你可以根据业务需求进行进一步处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温暖前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值