Skip to content

Upload

文件上传
基于 axiostype="file" input 实现

文档使用 JSONPlaceholder 做 demo 服务使用

Note 注意

💕💕💕

自定义类型

ts
type UploadFileStatus = 'ready' | 'uploading' | 'success' | 'error';

interface UploadFile {
  uid: string;
  size: number;
  name: string;
  status?: UploadFileStatus;
  percent?: number;
  raw?: File;
  response?: any;
  error?: any;
}

interface DuckTyping {
  [key: string]: any;
}

基础用法

❣️view the code❣️

vue
<xyUpload action="https://jsonplaceholder.typicode.com/posts">
  <xyIcon icon="fa-solid fa-circle-up" size="9x"></xyIcon>
</xyUpload>

支持文件拖放

❣️view the code❣️

vue
<xyUpload action="https://jsonplaceholder.typicode.com/posts" drag>
  <xyIcon icon="fa-solid fa-circle-up" size="9x"></xyIcon>
</xyUpload>

设置默认已上传列表

  • ready.js
  • uploading.js
    50%
  • success.js
  • error.js

❣️view the code❣️

vue
<xyUpload action="https://jsonplaceholder.typicode.com/posts" drag :defaultFileList="defaultFileList">
  <xyIcon icon="fa-solid fa-circle-up" size="9x"></xyIcon>
</xyUpload>

ts
<script setup lang="ts" >
import {ref} from "vue"
type UploadFileStatus = 'ready' | 'uploading' | 'success' | 'error';

interface UploadFile {
  uid: string;
  size: number;
  name: string;
  status?: UploadFileStatus;
  percent?: number;
  raw?: File;
  response?: any;
  error?: any;
}
const defaultFileList =  ref<UploadFile[]>([{
  name: "ready.js",
  size: 1225,
  status: "ready",
  uid: "1677036555013upload-file",
},{
  name: "uploading.js",
  size: 1225,
  status: "uploading",
  percent: 50,
  uid: "1677036555013upload-file",
},{
  name: "success.js",
  size: 1225,
  status: "success",
  uid: "1677036555012upload-file",
},{
  name: "error.js",
  size: 1225,
  status: "error",
  uid: "1677036555013upload-file",
}]);
</script>

文件上传的生命周期回调

JSONPlaceholder 对稍大点的文件就会返回上传失败

❣️view the code❣️

vue
<xyUpload action="https://jsonplaceholder.typicode.com/posts" drag :beforeUpload="beforeUpload" :onProgress="onProgress" :onSuccess="onSuccess" :onError="onError" :onChange="onChange" :onRemove="onRemove">
  <xyIcon icon="fa-solid fa-circle-up" size="9x"></xyIcon>
</xyUpload>

ts
<script setup lang="ts">
const beforeUpload = (file)=>{
  // 上传前对文件做校验判断是否可以上传
  console.log("beforeUpload", file)
  return true
}
const onProgress = (percentage, file)=>{
  // 文件上传的进度
  console.log("onProgress", percentage, file)
}
const onSuccess = (data, file)=>{
  // 文件上传成功
  console.log("onSuccess", data, file)
}
const onError = (err, file)=>{
  // 文件上传失败
  console.log("onError", err, file)
}
const onChange = (file)=>{
  // 文件上传成功或失败都会触发onChange(文件列表发生变化)
  console.log("onChange", file)
}
const onRemove = (file)=>{
  // 文件列表移除文件
  console.log("onRemove", file)
}
</script>

Props 属性

属性名属性类型说明默认值
actionstring上传文件的服务器地址-
dragboolean是否支持拖放文件false
classNamestring自定义CSS类名-
defaultFileListUploadFile[]默认已上传文件列表-
beforeUpload(file: File) => boolean | Promise<File>上传前的回调函数-
onProgress(percentage: number, file: UploadFile) => void上传中的回调函数-
onSuccess(data: any, file: UploadFile) => void上传成功的回调函数-
onError(err: any, file: UploadFile) => void上传失败的回调函数-
onChange(file: UploadFile) => void选择文件的回调函数-
onRemove(file: UploadFile) => void移除上传列中的文件回调函数-
headersDuckTypingaxios headers 配置-
dataDuckTypingformData data 添加-
withCredentialsboolean是否需要忽略响应中的 cookie-
namestring上传文件类型设置file
acceptstring文件上传控件中预期文件类型的提示-
multipleboolean是否允许多选-