vue-cli项目中,配合element_ui来实现上传图片与视频到oss上。
<template> <div class="basicInfo"> <el-upload class="avatar-content" v-loading="fileLoading" accept="image/*" drag action="https://zxcity-app.oss-cn-hangzhou.aliyuncs.com" :show-file-list="false" :data="ossParams" :before-upload="checkParams" :on-progress="progress" :on-error="uploadErr" :on-success="uploadSuccess" :on-remove="fileListRemove" multiple > </el-upload> <div v-for="(item,index) in fileList" :key="index" class="imgDiv"> <img :src="item.imgUrl" alt=""> <p>{{item.progress}}</p> </div> </div> </template> <script> import axios from 'axios' export default { data () { return { form: { url: '' }, fileList: [], fileLoading: false, ossParams: { expireTime: '', key: '', dir: '' } } }, methods: { // 图片上传前检测参数变化 checkParams (file) { var _this = this var promise = new Promise((resolve, reject) => { axios.get('https://share.zxtest.izxcs.com/zxcity_restful/ws/oss/ossUpload', {}) .then(function (response) { var params = response.data _this.ossParams = params _this.ossParams.name = file.name _this.ossParams.OSSAccessKeyId = params.accessid _this.ossParams.success_action_status = '200' _this.ossParams.key = params.dir + '/' + _this.getUUID() var obj = { name: _this.ossParams.name, key: _this.ossParams.key, host: _this.ossParams.host, progress: 0, imgUrl: '' } _this.fileList.push(obj) // _this.fileLoading = true resolve() }) .catch(function (error) { console.log(error, '错误') reject(error) }) }) return promise }, // 上传中 progress (event, file, fileList) { console.log('上传中...') console.log(file) console.log(fileList) this.fileList.forEach((item, index) => { if (item.name === file.name) { item.progress = parseInt(file.percentage) } }) }, // 上传失败提示 uploadErr (res) { this.$message.error('上传出错!') }, // 上传成功后上传到file表 uploadSuccess (response, file, fileList) { console.log('上传成功') this.fileList.forEach((item, index) => { if (item.name === file.name) { item.imgUrl = item.host + '/' + item.key item.progress = 100 } }) }, // 文件删除 fileListRemove (file, fileList) { this.form.url = '' }, // 随机名称 getUUID () { return `${this.str4()}${this.str4()}-${this.str4()}-${this.str4()}-${this.str4()}-${this.str4()}${this.str4()}${this.str4()}` }, str4 () { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1) } } } </script> <style lang="less" scoped> .imgDiv{ display: block; float: left; width: 80px; height: 100px; border: 2px solid black; img{ display: block; width: 50px; height: 80px; } p{ font-size: 14px; text-align: center; } } </style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现