代码
@{
ViewBag.Title = "上传视频到OSS";
}
<link href="~/Scripts/element-ui/elementui-index.css" rel="stylesheet" />
<div id="upload">
<template>
<div>
<div class="wrap">
<div>视频:</div>
<div><input type="file" id="uploadImage" @@change="toUpload" placeholder="" /></div>
</div>
</div>
</template>
</div>
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/Scripts/vue.js"></script>
<script src="~/Scripts/element-ui/elementui-index.js"></script>
//阿里云oss-sdk
<script src="https://www.promisejs.org/polyfills/promise-6.1.0.js"></script>
<script src="https://gosspublic.alicdn.com/aliyun-oss-sdk-4.4.4.min.js"></script>
<script>
//import OSS from 'ali-oss';
var pcLiveVm = new Vue({
el: "#upload",
data: function () {
return {
loading: false
};
},
methods: {
toUpload() {
let _this = this;
_this.loading = true
var client = new OSS.Wrapper({
region: '区域', //如果是北京区就是oss-cn-beijing
accessKeyId: '你OSS的accessKeyId',
accessKeySecret: '你OSS的accessKeySecret',
bucket: 'bucket名称',
endpoint:'地域节点' //需要加上https://,比如https://oss-cn-beijing.aliyuncs.com
});
//获取文件信息
const files = document.getElementById("uploadImage")
if (files.files) {
const fileLen = document.getElementById("uploadImage").files
const file = document.getElementById("uploadImage").files[0]
let consat = file.name;
let name = fileLen[0].name;
for (let i = 0; i < fileLen.length; i++) {
const file = fileLen[i];
//如果要将文件保存到bucket下的二级目录liveVideo下,需要在name前添加“liveVIdeo/”
client.multipartUpload("liveVideo/"+name, file).then(function (res) {
_this.loading = false;
var str = res.res.requestUrls[0];
console.log(str.split("?")[0]);
_this.$emit('childByValue', str.split("?")[0]);
console.log(res);
}).catch(function (err) {
console.log(err);
})
}
}
},
},
});
</script>
<style scoped>
.wrap {
width: 350px;
height: 40px;
display: flex;
justify-content: space-between;
}
</style>
解决本地调试跨域问题
设置跨域规则:找到OSS存储——Bucket列表——基础设置——跨域访问——设置
image.png
暴露Headers设置:etag x-oss-request-id
image.png