Replies: 2 comments
-
补充几点:
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Upload组件优化方案Upload 上传文件上传组件。 何时使用当需要将文件上传到后端服务器时。 基本用法:::demo <template>
<d-upload v-model="uploadedFiles" :upload-options="uploadOptions" />
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const uploadedFiles = ref([]);
const uploadOptions = ref({
uri: 'https://run.mocky.io/v3/132b3ea3-23ea-436b-aed4-c43ef9d116f0',
});
return {
uploadedFiles,
uploadOptions,
};
},
};
</script> ::: 多文件上传:::demo <template>
<d-upload v-model="uploadedFiles" :upload-options="uploadOptions" multiple />
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const uploadedFiles = ref([]);
const uploadOptions = ref({
uri: 'https://run.mocky.io/v3/132b3ea3-23ea-436b-aed4-c43ef9d116f0',
});
return {
uploadedFiles,
uploadOptions,
};
},
};
</script> ::: 拖动文件上传:::demo <template>
<d-upload v-model="uploadedFiles" :upload-options="uploadOptions" droppable />
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const uploadedFiles = ref([]);
const uploadOptions = ref({
uri: 'https://run.mocky.io/v3/132b3ea3-23ea-436b-aed4-c43ef9d116f0',
});
return {
uploadedFiles,
uploadOptions,
};
},
};
</script> ::: 禁止上传:::demo <template>
<d-upload v-model="uploadedFiles" :upload-options="uploadOptions" disabled />
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const uploadedFiles = ref([]);
const uploadOptions = ref({
uri: 'https://run.mocky.io/v3/132b3ea3-23ea-436b-aed4-c43ef9d116f0',
});
return {
uploadedFiles,
uploadOptions,
};
},
};
</script> ::: 任意区域上传用户可通过默认 slot 支持文件任意区域上传。 :::demo <template>
<d-upload
class="upload-demo"
accept=".png"
v-model="uploadedFiles"
:upload-options="uploadOptions"
:droppable="true"
:on-success="onSuccess"
:on-error="onError"
:before-upload="beforeUpload"
@file-over="fileOver"
@file-drop="fileDrop"
@file-select="fileSelect"
@delete-uploaded-file="deleteUploadedFile"
>
<div class="upload-trigger">
<div>
<d-icon name="upload" size="24px" />
</div>
<div style="marginTop: 20px">
将文件拖到此处,或
<span class="link">点击上传</span>
</div>
</div>
<template v-slot:uploaded-files="slotProps">
<table class="table uploaded-files" v-if="slotProps.uploadedFiles.length > 0">
<tbody>
<tr v-for="(uploadedFile, index) in slotProps.uploadedFiles" :key="index" class="row">
<td width="50%">
<span>{{ uploadedFile.name }}</span>
</td>
<td width="25%">
<span>Uploaded</span>
</td>
<td>
<d-button size="xs" @click="(event) => slotProps.deleteFile(uploadedFile)">
Delete
</d-button>
</td>
</tr>
</tbody>
</table>
</template>
</d-upload>
</template>
<script>
import { ref, watch } from 'vue';
export default {
setup() {
const uploadedFiles = ref([
{
name: 'food.jpeg',
url: 'https://devui.design/home',
},
{
name: 'food2.jpeg',
url: 'https://devui.design/home',
},
]);
const uploadOptions = ref({
uri: 'https://run.mocky.io/v3/132b3ea3-23ea-436b-aed4-c43ef9d116f0',
});
const beforeUpload = (file) => {
console.log(file);
return true;
};
const onSuccess = (result) => {
console.log('success', result);
};
const onError = (error) => {
console.log(error);
};
const fileOver = (fileInfo) => {
console.log('fileInfo:', fileInfo);
};
const fileDrop = (fileInfo) => {
console.log('fileInfo:', fileInfo);
};
const fileSelect = (fileInfo) => {
console.log('fileInfo:', fileInfo);
};
const deleteUploadedFile = (fileInfo) => {
console.log('fileInfo:', fileInfo);
};
watch(uploadedFiles, (newValue, oldValue) => {
console.log('uploadedFiles', {
newValue,
oldValue,
});
});
return {
uploadedFiles,
uploadOptions,
beforeUpload,
onSuccess,
onError,
fileOver,
fileDrop,
fileSelect,
deleteUploadedFile,
};
},
};
</script>
<style>
.upload-demo .upload-trigger {
background-color: #fff;
border: 1px dashed #d9d9d9;
border-radius: 6px;
box-sizing: border-box;
width: 360px;
height: 180px;
text-align: center;
cursor: pointer;
position: relative;
overflow: hidden;
display: flex;
flex-direction: column;
justify-content: center;
}
.upload-demo .upload-trigger .link {
color: #5e7ce0;
}
</style> ::: d-upload 参数
d-upload 事件
d-upload 插槽
IUploadOptions 类型export class IUploadOptions {
// 上传接口地址
uri: string;
// http 请求方法
method?: string;
// 上传文件大小限制
maximumSize?: number;
// 自定义请求headers
headers?: { [key: string]: any };
// 认证token
authToken?: string;
// 认证token header标示
authTokenHeader?: string;
// 上传额外自定义参数
additionalParameter?: { [key: string]: any };
// 上传文件字段名称,默认file
fileFieldName?: string;
// 多文件上传,是否检查文件重名,设置为true,重名文件不会覆盖,否则会覆盖上传
checkSameName?: boolean;
// 指示了是否该使用类似cookies,authorization headers(头部授权)
// 或者TLS客户端证书这一类资格证书来创建一个跨站点访问控制(cross-site Access-Control)请求
withCredentials?: boolean;
// 手动设置返回数据类型
responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
api
Upload组件:https://vue-devui.github.io/components/upload/
before-xxx
或after-xxx
方式命名Beta Was this translation helpful? Give feedback.
All reactions