oss上传文件阿里云(js版本)

<input type="file" name="picFieldName" id="picFieldId" οnchange="uploadPic(this)" />
<!-- oss 上传文件 JavaScript 库 -->
<script src="http://gosspublic.alicdn.com/aliyun-oss-sdk-4.4.4.min.js"></script>
<script>
    var client = new OSS.Wrapper({
        region : 'oss-cn-beijing',
        accessKeyId : 'LTAIshUKIQ2d2h',
        accessKeySecret : 'KlA92t0ABZix1taZd5lxWyAkNFS2',
        bucket : 'demo'
    });
    function uploadPic(obj){
        var file=obj.files[0];//获取文件流
        var val= obj.value;
        var suffix = val.substr(val.indexOf("."));
        var storeAs = "demo/"+timestamp()+suffix;
        console.log(file.name + ' => ' + storeAs);
        client.multipartUpload(storeAs, file).then(function (result) {

        }).catch(function (err) {
            console.log(err);
        });
    }

    /**
     * 生成文件名
     * @returns
     */
    function timestamp(){
        var time = new Date();
        var y = time.getFullYear();
        var m = time.getMonth()+1;
        var d = time.getDate();
        var h = time.getHours();
        var mm = time.getMinutes();
        var s = time.getSeconds();
        console.log(y);
        return ""+y+add0(m)+add0(d)+add0(h)+add0(mm)+add0(s);
    }
    function add0(m){
        return m<10?'0'+m : m;
    }
</script>

猜你喜欢

转载自blog.51cto.com/13640989/2598992

相关文章