1. 首先安装oss
npm install ali-oss --save
2.
// template部分 <Upload ref="upload" type="drag" :default-file-list="file" action="" :before-upload="handleBeforeUpload" :on-preview="handlePreview" :on-remove="handleRemove" :accept="accept" with-credentials> <div style="padding: 20px 0"> <Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon> <p>点击或拖拽上传文件</p> </div> </Upload> <Progress :percent="uploadPercent" v-if="isPercent"/>
3. script部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | <script> export deault { data () { return { file: [], accept: 'video/*' , ossConfig: {}, ossInfo: {}, isPercent: false , uploadPercent: 0, format:[], maxSize: 1 * 1024 * 1024 } }, methods: { // 获取上传凭证 async getUploadToken () { // 通过后端接口获取上传基本信息 const res = await getUploadInfo(params) this .ossConfig = { region: res.data.region_id, accessKeyId: res.data.AccessKeyId, accessKeySecret: res.data.AccessKeySecret, stsToken: res.data.SecurityToken, bucket: res.data.bucket, secure: true } this .ossInfo = { Expiration: res.data.Expiration, dir: res.data.dir, domain: res.data.domain, filename: res.data.filename } }, // 定义上传方法。 async multipartUpload (filename, file) { try { const result = await this .client.multipartUpload(`${ this .ossInfo.dir}${ this .ossInfo.filename}.${ this .fileSuffix}`, file, { headers: { 'Content-Disposition' : 'inline' , 'Content-Type' : this .fileType }, progress: (p, checkpoint) => { this .isPercent = true this .uploadPercent = Number((p * 100).toFixed(2)) // checkpoint参数用于记录上传进度,断点续传上传时将记录的checkpoint参数传入即可。浏览器重启后无法直接继续上传,您需要手动触发上传操作。 this .tempPoint = checkpoint }, parallel: 10, // 设置分片大小 默认是1M,这里设置5M partSize: 1024 * 1024 * 5, mime: this .fileType }) if (result.res.statusCode !== 200) { this .$Message.error( '文件上传失败' ) return false } this .file = [ { name: this .fileName, url: this .ossInfo.domain + result.name } ] this .$Message.success(`文件上传成功`) this .isPercent = false } catch (e) { this .$Message.error( '文件上传失败' ) this .isPercent = false } }, // 上传前处理 handleBeforeUpload (file, fileList) { this .fileSuffix = file.name.split( '.' ).pop() this .fileName = file.name this .fileType = file.type if (! this .format.includes( this .fileSuffix)) { this .$Message.error(`上传文件仅支持${ this .format.toString()}格式`) return false } if (file.size > this .maxSize) { this .$Message.error(`当前上传文件最大不能超过100M`) return false } this .client = new OSS( this .ossConfig) this .multipartUpload(file.name, file) return false }, } } </script> |
分类:
view-design
, vue
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具