vue-apple-file-upload
Apple 风格 Vue 2 文件上传组件,内置环形进度动画、文件列表、大小/数量校验。
安装
npm i vue-apple-file-upload
假设你的后端上传接口是 POST https://api.example.com/upload,接收 multipart/form-data,字段名 file。
方式一:全局注册(推荐)
① 在 main.js 注册并传入 axios 实例
// src/main.js
import Vue from "vue";
import axios from "axios";
import AppleFileUpload from "vue-apple-file-upload";
const http = axios.create({
baseURL: "https://api.example.com",
timeout: 120000,
});
Vue.use(AppleFileUpload, { http });
② 在任意页面使用
<template>
<apple-file-upload
upload-url="/upload"
:simulate="false"
@success="onSuccess"
@error="onError"
/>
</template>
<script>
export default {
methods: {
onSuccess(file) {
console.log("成功", file.name);
},
onError(err) {
console.error("失败", err);
},
},
};
</script>
实际请求地址 = axios baseURL + upload-url = https://api.example.com/upload。
方式二:局部注册
① 在页面中引入组件并传入 axios 实例
<template>
<apple-file-upload
:http="myAxios"
upload-url="/upload"
:simulate="false"
@success="onSuccess"
/>
</template>
<script>
import axios from "axios";
import { AppleFileUpload } from "vue-apple-file-upload";
export default {
components: { AppleFileUpload },
data() {
return {
myAxios: axios.create({ baseURL: "https://api.example.com" }),
};
},
methods: {
onSuccess(file) {
console.log("成功", file.name);
},
},
};
</script>
Props
| 参数 |
类型 |
默认值 |
说明 |
upload-url |
String |
'' |
上传接口路径(相对路径会拼在 axios baseURL 后) |
multiple |
Boolean |
true |
是否允许多选 |
file-types |
String |
'' |
accept 语法,如 .jpg,.png,空串不限 |
webkitdirectory |
Boolean |
false |
是否选择文件夹 |
slice-size |
Number |
0 |
分片大小(字节),0 不分片 |
md5 |
Boolean |
false |
是否计算并上传 MD5 |
max-file-size |
Number |
104857600 |
单文件上限(字节),默认 100MB |
max-file-count |
Number |
0 |
最多文件数,0 不限 |
simulate |
Boolean |
true |
true 模拟上传(预览动画),false 走真实接口 |
Events
| 事件 |
回调参数 |
说明 |
success |
(file: File) |
单个文件上传成功 |
error |
(err: Error) |
上传失败 |
warn |
(msg: String) |
校验提示(超大小/超数量) |
请求格式
- 整文件上传:
POST {uploadUrl},FormData 字段 file
- 分片上传:
POST {uploadUrl},FormData 字段 file、filename、chunkNumber、totalChunks
- 进度:通过 axios
onUploadProgress 拿到 loaded/total
License
MIT