获取七牛token

1. 项目求上传照片到七牛云

//获取骑牛token
function getQNToken() {
    //localStorage.removeItem('QNToken');
    var token = $.getLocal('QNToken');
    if (!token || (!!token && Math.round(new Date().getTime() / 1000) >= token.expires)) {
        $.send({ url: '/Api/Qiniu/GetToken', async: false }, function (result) {
            if (result.status == 200) {
                token = result.data;
                $.setLocal('QNToken', result.data);
            }
        });
    }
    //console.log(Math.round(new Date().getTime() / 1000));
    //console.log(token);
    return token.token;
}
function guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

2. 上传

 var token = getQNToken();
                console.log(token);
                var f = document.getElementById("bannerUpload").files[0];
                var data = new FormData();
                data.append("file", f);
                data.append("key", guid() + '.' + f.type.substring(6, f.type.length));
                data.append("token", token);
                $.ajax({
                    data: data,
                    type: "POST",
                    url: "http://upload.qiniu.com",
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function (data) {
                        
                        $('.bannerMage').val('http://地址/'+data.key)
                    }
                })

 

  3.结束

posted @ 2017-03-08 16:26  Terre  阅读(5209)  评论(0编辑  收藏  举报

风光无限好