Upload上传·续2
<template> <a-upload-dragger v-model:fileList="fileList" name="file" :multiple="true" action="https://www.mocky.io/v2/5cc8019d300000980a055e76" @change="handleChange" > <p class="ant-upload-drag-icon"> <inbox-outlined></inbox-outlined> </p> <p class="ant-upload-text">Click or drag file to this area to upload</p> <p class="ant-upload-hint"> Support for a single or bulk upload. Strictly prohibit from uploading company data or other band files </p> </a-upload-dragger></template><script lang="ts">import { InboxOutlined } from '@ant-design/icons-vue';import { message } from 'ant-design-vue';import { defineComponent, ref } from 'vue';interface FileItem { uid: string; name?: string; status?: string; response?: string; url?: string;}interface FileInfo { file: FileItem; fileList: FileItem[];}export default defineComponent({ components: { InboxOutlined, }, setup() { const handleChange = (info: FileInfo) => { const status = info.file.status; if (status !== 'uploading') { console.log(info.file, info.fileList); } if (status === 'done') { message.success(`${info.file.name} file uploaded successfully.`); } else if (status === 'error') { message.error(`${info.file.name} file upload failed.`); } }; return { handleChange, fileList: ref([]), }; },});</script>
手动上传
beforeUpload 返回 false 后,手动上传文件。
<template> <div class="clearfix"> <a-upload :file-list="fileList" :remove="handleRemove" :before-upload="beforeUpload" v-model:file-list="fileList" > <a-button> <upload-outlined></upload-outlined> Select File </a-button> </a-upload> <a-button type="primary" :disabled="fileList.length === 0" :loading="uploading" style="margin-top: 16px" @click="handleUpload" > {{ uploading ? 'Uploading' : 'Start Upload' }} </a-button> </div></template><script lang="ts">import request from 'umi-request';import { UploadOutlined } from '@ant-design/icons-vue';import { message } from 'ant-design-vue';import { defineComponent, ref } from 'vue';interface FileItem { uid: string; name?: string; status?: string; response?: string; url?: string; preview?: string; originFileObj?: any; file: string | Blob;}export default defineComponent({ components: { UploadOutlined, }, setup() { const fileList = ref<FileItem[]>([]); const uploading = ref<boolean>(false); const handleRemove = (file: FileItem) => { const index = fileList.value.indexOf(file); const newFileList = fileList.value.slice(); newFileList.splice(index, 1); fileList.value = newFileList; }; const beforeUpload = (file: FileItem) => { fileList.value = [...fileList.value, file]; return false; }; const handleUpload = () => { const formData = new FormData(); fileList.value.forEach(({ file }: FileItem) => { formData.append('files[]', file); }); uploading.value = true; // You can use any AJAX library you like request('https://www.mocky.io/v2/5cc8019d300000980a055e76', { method: 'post', data: formData, }) .then(() => { fileList.value = []; uploading.value = false; message.success('upload successfully.'); }) .catch(() => { uploading.value = false; message.error('upload failed.'); }); }; return { fileList, uploading, handleRemove, beforeUpload, handleUpload, }; },});</script>
自定义预览
自定义本地预览,用于处理非图片格式文件(例如视频文件)。
<template> <div> <a-upload list-type="picture" action="//jsonplaceholder.typicode.com/posts/" :preview-file="previewFile" v-model:file-list="fileList" > <a-button> <upload-outlined></upload-outlined> Upload </a-button> </a-upload> </div></template><script lang="ts">import { UploadOutlined } from '@ant-design/icons-vue';import { defineComponent, ref } from 'vue';export default defineComponent({ components: { UploadOutlined, }, setup() { const previewFile = async (file: any): Promise<Response> => { console.log('Your upload file:', file); // Your process logic. Here we just mock to the same file const res = await fetch('https://next.json-generator.com/api/json/get/4ytyBoLK8', { method: 'POST', body: file, }); const { thumbnail } = await res.json(); return thumbnail; }; return { previewFile, fileList: ref([]), }; },});</script>
API
| accept |
接受上传的文件类型, 详见 input accept Attribute |
string |
无 |
|
|
|
|
|
|
| action | 上传的地址 | string|(file) => Promise | 无 | |
|---|---|---|---|---|
| method |
上传请求的 http method |
string |
‘post’ |
1.5.0 |
|
|
|
|
|
| directory |
支持上传文件夹(caniuse) |
boolean |
false |
|
|
|
|
|
|
| beforeUpload | 上传文件之前的钩子,参数为上传的文件,若返回 false 则停止上传。支持返回一个 Promise 对象,Promise 对象 reject 时则停止上传,resolve 时开始上传( resolve 传入 File 或 Blob 对象则上传 resolve 传入对象)。注意:IE9 不支持该方法。 | (file, fileList) => boolean | Promise | 无 | |
|---|---|---|---|---|
| customRequest |
通过覆盖默认的上传行为,可以自定义自己的上传实现 |
Function |
无 |
|
|
|
|
|
|
| data | 上传所需参数或返回上传参数的方法 | object|(file) => object | 无 | |
|---|---|---|---|---|
| disabled |
是否禁用 |
boolean |
false |
|
|
|
|
|
|
| fileList |
已经上传的文件列表(受控) |
object[] |
无 |
|
|
|
|
|
|
| headers |
设置上传的请求头部,IE10 以上有效 |
object |
无 |
|
|
|
|
|
|
| listType |
上传列表的内建样式,支持三种基本样式 text, picture 和 picture-card |
string |
‘text’ |
|
|
|
|
|
|
| multiple |
是否支持多选文件,ie10+ 支持。开启后按住 ctrl 可选择多个文件。 |
boolean |
false |
|
|
|
|
|
|
| name |
发到后台的文件参数名 |
string |
‘file’ |
|
|
|
|
|
|
| previewFile | 自定义文件预览逻辑 | (file: File | Blob) => Promise<dataURL: string> | 无 | 1.5.0 |
|---|---|---|---|---|
| showUploadList |
是否展示 uploadList, 可设为一个对象,用于单独设定 showPreviewIcon 和 showRemoveIcon |
Boolean or { showPreviewIcon?: boolean, showRemoveIcon?: boolean } |
true |
|
|
|
|
|
|
| supportServerRender |
服务端渲染时需要打开这个 |
boolean |
false |
|
|
|
|
|
|
| withCredentials |
上传请求时是否携带 cookie |
boolean |
false |
|
|
|
|
|
|
| openFileDialogOnClick |
点击打开文件对话框 |
boolean |
true |
|
|
|
|
|
|
| remove | 点击移除文件时的回调,返回值为 false 时不移除。支持返回一个 Promise 对象,Promise 对象 resolve(false) 或 reject 时不移除。 | Function(file): boolean | Promise | 无 | |
|---|---|---|---|---|
| transformFile | 在上传之前转换文件。支持返回一个 Promise 对象 | Function(file): string | Blob | File | Promise<string | Blob | File> | 无 | 1.5.0 |
|---|---|---|---|---|
事件
| change |
上传文件改变时的状态,详见 change |
Function |
无 |
|
|
|
|
| preview |
点击文件链接或预览图标时的回调 |
Function(file) |
无 |
|
|
|
|
| download |
点击下载文件时的回调,如果没有指定,则默认跳转到文件 url 对应的标签页。 |
Function(file): void |
跳转新标签页 |
|
|
|
|
| reject |
拖拽文件不符合 accept 类型时的回调 |
Function(fileList) |
无 |
|
|
|
|
change
上传中、完成、失败都会调用这个函数。
文件状态改变的回调,返回为:
{ file: { /* ... */ }, fileList: [ /* ... */ ], event: { /* ... */ },}
-
file 当前操作的文件对象。
{ uid: 'uid', // 文件唯一标识,建议设置为负数,防止和内部产生的 id 冲突 name: 'xx.png', // 文件名 status: 'done', // 状态有:uploading done error removed response: '{"status": "success"}', // 服务端响应内容 linkProps: '{"download": "image"}', // 下载链接额外的 HTML 属性 xhr: 'XMLHttpRequest{ ... }', // XMLHttpRequest Header}
-
fileList 当前的文件列表。
-
event 上传中的服务端响应内容,包含了上传进度等信息,高级浏览器支持。