$.ajax({
url: uptoken
})
.done(function(data) {
var uploader = Qiniu.uploader({
runtimes: 'html5,flash,html4', //上传模式,依次退化
browse_button: 'pickfiles', //上传选择的点选按钮,**必需**
uptoken: data.token, //uptoken
domain: 'http://7xo7gu.com1.z0.glb.clouddn.com/', //bucket 域名,下载资源时用到,**必需**
get_new_uptoken: false, //设置上传文件的时候是否每次都重新获取新的token
max_file_size: '10mb', //最大文件体积限制
flash_swf_url: 'plupload/Moxie.swf', //引入flash,相对路径
max_retries: 3, //上传失败最大重试次数
chunk_size: '4mb', //分块上传时,每片的体积
filters: {
mime_types: [{
title: "语音文件",
extensions: "mp3"
}]
},
auto_start: true, //选择文件后自动上传,若关闭需要自己绑定事件触发上传,
init: {
QueueChanged: function(up) {
//自定义路径
for (var i = 0; i < up.files.length; i++) {
up.files[i].name = data.paths['brand.audio'] + md5(up.files[i].name) + '.' + up.files[i].name.split('.')[up.files[i].name.split('.').length - 1];
path = up.files[i].name;
}
},
FileUploaded: function(up, file, info) {
var domain = up.getOption('domain');
var res = $.parseJSON(info);
var sourceLink = domain + res.key; //获取上传成功后的文件的Url
console.log('上传成功');
// sourceLink = path; 使用自定义路径
},
UploadProgress: function() {
$('#pickfiles').prop('disabled', true);
console.log('上传中...');
},
Error: function(up, err, errTip) {
console.log('上传失败');
},
UploadComplete: function() {
$('#pickfiles').prop('disabled', false);
console.log('上传完成');
}
}
});
});
see more: qiniu js-sdk