<template>
<el-upload
v-model:file-list="fileList"
action="http://upload-z2.qiniup.com" //七牛云华南地址
list-type="picture-card"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove"
:auto-upload="true"
:before-upload="beforeAvatarUpload"
:on-success="handleAvatarSuccess"
:data="uploadData"
:limit="limit"
class="avatar-uploader"
>
<el-icon>
<Plus />
</el-icon>
<template #tip>
<div class="el-upload__tip">
{{ tip }}
</div>
</template>
</el-upload>
<el-dialog v-model="dialogVisible">
<img w-full :src="dialogImageUrl" alt="Preview Image" />
</el-dialog>
</template>
<script lang="ts" setup>
import { ref, onMounted ,watch} from "vue";
import { Plus } from "@element-plus/icons-vue";
import { ElMessage, ElUpload } from "element-plus";
import type { UploadProps, UploadUserFile } from "element-plus";
import { getQnyToken } from "@/api/userCore/qny";
const props = defineProps({
imgUrl: {
type: String,
default: "",
},
// 数量限制
limit: {
type: Number,
default: 1,
},
tip: {
type: String,
default:
"建议图片比例16:9;只能上传jpg/png格式图片;单张图不超过2MB;最多上传三张",
},
});
//#region 侦听器
watch(() => props.imgUrl, (newValue, oldValue) => {
if (newValue) {
let imgList = newValue.split(",")
fileList.value = imgList.map((item) => {
return {
name: "",
url: item
}
})
}
})
//#endregion
// 图片列表
const fileList: any = ref<UploadUserFile[]>([]);
// url列表
const picURL: any = ref([]);
const dialogImageUrl = ref("");
const dialogVisible = ref(false);
const uploadData: any = ref({
token: "", //七牛云token
key: null,
});
const emit = defineEmits(["uploadSuccessEmits", "importTemplateEmits"]);
//页面初始化
onMounted(() => {
getQnyToken().then((res) => {
uploadData.value.token = res.data;
});
});
// 删除图片
const handleRemove: UploadProps["onRemove"] = (uploadFile, uploadFiles) => {
console.log(uploadFile, uploadFiles);
console.log(fileList.value, 111);
picURL.value = fileList.value.map((item: any) => {
return {
name: item.name,
url: item.url,
};
});
emit("uploadSuccessEmits", picURL.value);
};
// 放大
const handlePictureCardPreview: UploadProps["onPreview"] = (uploadFile) => {
dialogImageUrl.value = uploadFile.url!;
dialogVisible.value = true;
};
// 上传之前
const beforeAvatarUpload = (file: any) => {
//判断图片大小
const isLt2M = file.size / 1024 / 1024 < 2;
const fileName = file.name;
if (!isLt2M) {
ElMessage.error("图片必须小于5MB!");
}
// //判断图片类型
let types = ["image/jpg", "image/png"];
const isImage = types.includes(file.type);
if (!isImage) {
ElMessage.error("只能上传jpg/png!");
}
//文件名称
uploadData.value.key = fileName;
return isLt2M && isImage;
};
// 上传成功
const handleAvatarSuccess = (res: any, file: any) => {
picURL.value.push({
name: uploadData.value.key,
url: `${`https://qny.xxxxxxx.com/${uploadData.value.key}`}`, //七牛云配置的域名地址
});
console.log(fileList.value, "图片列表");
console.log(picURL.value, "url列表");
emit("uploadSuccessEmits", picURL.value);
};
</script>
<style scoped>
.avatar-uploader .avatar {
width: 150px;
height: 150px;
display: block;
}
</style>
el-upload 上传七牛云
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 参考 https://blog.csdn.net/qq_36710522/article/details/9954...
- egg-multipart有两种模式:file和streamel-upload参数传递有两种方式:利用自带参数da...
- 1.前提条件 已开通阿里云对象存储OSS服务。已创建RAM用户的AccessKey ID和AccessKey Se...
- 1、首先安装依赖npm install ali-oss2、封装client 3、页面使用 HTML 因为样式为自定...
- api接口处添加属性// 校验台账export const checkEquiment = (data) => {...