当前位置: 首页 > news >正文

vue+iviewUi+oss直传阿里云上传文件

c、初始化OSS对象:

this.client = new OSS({region: '',//地域(在创建 Bucket 的时候指定的中心位置),这里可能不知道具体地域怎么填其实就是 oss-cn-中心位置 ,例:region:'oss-cn-chengdu',chengdu则是创建bucket是指定的位置成都。accessKeyId: '', //阿里云产品的通用idaccessKeySecret: '',//密钥bucket: '' //OSS 存储区域名
});

d、定义选取文件上传到oss的方法

uploadFile(event){let file = event.target.files[0]if(!(/^\S+\.mp4$/.test(file.name))){return this.$message.error('请上传视频文件')}/*** 文件的类型,判断是否是视频*/let param = new FormData()param.append('file', file, file.name);console.log('开始上传')this.put(file.name,file)
},

e、定义put方法上传到阿里云

async put (name,file) {try {var fileName = new Date().getTime()+name;//object-name可以自定义为文件名(例如file.txt)或目录(例如abc/test/file.txt)的形式,实现将文件上传至当前Bucket或Bucket下的指定目录。let result = await this.client.put(fileName, file);this.video_url=result.url;//返回的上传视频地址//一下为生成图片处理的签名 URL t_1000表示第一秒视频图片,常用来作为视频封面图const imgRes = this.video_url+'?x-oss-process=video/snapshot,t_1000,f_jpg,w_0,h_0,m_fast';      } catch (e) {          console.log(e);}
},

可能遇到的问题:
1、跨域不能上传成功:
跨域不能上传成功里插入图片描述
去阿里云配置域名,上传服务器验证

uni-app中使用(需要后端配合一下)

1、data定义数据

data() {return {ossData:{accessid: "",dir: "/uploads/202003/",expire: 1585653811,host: "",policy: "",signature: ""},fileInfo:null,}
},

2、定义选择要上传的视频文件方法

selVideo(type){uni.chooseVideo({count: 1,maxDuration:15,compressed:false,success: (res) => {if(parseFloat(res.duration)>=16){return this.$toast('请选取小于15s的视频!')}let tempFilePath = res.tempFilePath;this.fileInfo=res;if(!this.fileInfo){return}uni.showLoading({title:'上传中...'})this.getOssSign(res.tempFilePath)	}							});
},

3、定义获取服务器端返回oss配置方法

async getOssSign(path,type){let [e, data] = await this.$api.getOssSign();if (e) returnif (data.errNum === 200) {										this.ossData=data.result;	let fileName=new Date().getTime()+'app'+this.fileInfo.tempFilePath.substr(this.fileInfo.tempFilePath.length-6,)uni.uploadFile({url: this.ossData.host,  //后台给的阿里云存储给的上传地址filePath: path,  fileType: 'video',name: 'file',formData: {key: fileName,  //文件名policy: this.ossData.policy,  //后台获取超时时间OSSAccessKeyId: this.ossData.accessid, //后台获取临时IDsuccess_action_status: '200', //让服务端返回200,不然,默认会返回204signature: this.ossData.signature //后台获取签名},success: (res) => {console.log(res,fileName);uni.hideLoading();uni.showToast({title: '上传成功',icon: 'success',duration: 1000});this.video=this.ossData.host+'/'+fileName;		},fail: (err) => {uni.hideLoading();uni.showModal({title: '上传失败',content: err.errMsg,showCancel: false});},complete:(com) => {console.log(com)}});				}else{this.$toast(data.errMsg);}
},
http://www.lryc.cn/news/117736.html

相关文章:

  • 算法leetcode|68. 文本左右对齐(rust重拳出击)
  • 基于MATLAB实现小波算法仿真(附上多个完整源码+数据集)
  • 【深度学习注意力机制系列】—— CBAM注意力机制(附pytorch实现)
  • 【资料分享】全志科技T507-H工业核心板规格书
  • Profibus-DP转modbus RTU网关modbus rtu和tcp的区别
  • AlmaLinux 9 安装 Edge 和 Chrome
  • NGINX——负载均衡
  • C#实现端口扫描和执行cmd命令、调用摄像头
  • 【图像恢复】基于交替乘子方法(ADMM)图像恢复算法研究[固定点收敛和应用](Matlab代码实现)
  • Qt 使用QLabel的派生类实现QLabel的双击响应
  • 关于@JSONField的使用
  • Centos7单机部署ElasticSearch
  • js玩儿爬虫
  • 新利好带动 POSE 持续上扬,月内几近翻倍
  • Windows terminal 添加 git bash 解决git中文乱码显示问题
  • C语言实现选择排序
  • unable to write symref for HEAD: Permission denied
  • 长时间序列遥感数据处理及在全球变化、物候提取、植被变绿与固碳分析、生物量估算与趋势分析等领域中的实践技术应用
  • 【行为型设计模式】C#设计模式之策略模式
  • Linux Shell 编程入门
  • Webstorm + Egg.js 进行断点调试
  • Oracle-创建PDB
  • 【TypeScript】交叉类型联合类型(四)
  • 数组和字符串-字符串
  • MySQL-索引基础
  • CentOS中自动加载802.1q模块
  • CSP-J2022第一轮试题
  • 使用Java根据表名导出与导入Sql
  • Elasticsearch同时使用should和must
  • 羽毛球热身和拉伸