C# Web直接上傳視頻或者文件到OSS

代碼

@{
    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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章