开始使用的是php版sdk,上传大文件时速度相对较慢,使用js版sdk通过请求服务器获得uptoken再进行上传,上传速度得到很大提升
https://developer.qiniu.com/kodo/sdk/1283/javascript
function upload_image(obj) {
var fileName = $(obj).val();
var suffix = fileName.substring(fileName.lastIndexOf("."),fileName.length);//后缀名
var file = $(obj).get(0).files[0];
$.ajax({url: "/api/GetQiniuUpToken", dataType:'json',success: function(result){
if(result.Code !== 200) {
return false;
}
var res = result.Result;
var token = res.UpToken;
var domain = res.Domain;
var observer = {
next(response){
//上传进度'+Math.floor(response.total.percent)+'%'
},
error(err){
//失败
},
complete(res1){
//完成
}
};
var key = res.UniqueKey + suffix;//上传文件名
var putExtra = {
fname: "",
params: {},
// mimeType:[]
mimeType:["image/png", "image/jpeg", "image/gif"]
};
var config = {};
var observable = qiniu.upload(file, key, token, putExtra, config)
observable.subscribe(observer) // 上传开始
}})
}