一、下载阿里云上传视频集成代码
包括aliyun-upload-sdk-1.5.0.min.js,aliyun-oss-sdk-5.3.1.min.js,以及es6-promise.min.js
二、封装公共方法实例化上传对象
;(function($){
$.fn.extend({
/**
* 视频上传
* @param {int} water 是否水印,默认false不加水印 预留,暂无效
* @returns {uploadVideo}
*/
uploadVideo: function(water) {
var water = water || false;
var uploadObject = this;
/*this.userData = '{"Vod":{"UserData":"{"IsShowWaterMark":'+water+',"Priority":"7"}"}}';*/
this.authData = {
UploadAddress: "",
VideoId: "",
RequestId: "",
UploadAuth: "",
};
//各种状态的回调
this._progressCallBack = null;
this._uploadedCallBack = null;
this._errorCallBack = null;
this._uploadBeforeCallBack = null;
/**
* 设置图片上传成功后的回调方法
* @param {function} fn 回调方法
* @returns {upload_files}
* @private
*/
this.setUploaded = function (fn) {
this._uploadedCallBack = fn;
return this;
};
/**
* 设置进度回调
* @param fn
* @returns {upload}
*/
this.setProgress = function (fn) {
this._progressCallBack = fn;
return this;
};
/**
* 设置开始上传前的回调方法
* @param {function} fn 回调方法
* @returns {upload_files}
* @private
*/
this.setUploadBefore = function (fn) {
this._uploadBeforeCallBack = fn;
return this;
};
/**
* 上传失败后的回调方法
* @param {function} fn 回调方法
* @returns {upload_files}
*/
this.setError = function (fn) {
this._errorCallBack = fn;
return this;
};
this.run = function () {
uploadObject[0].addEventListener ? uploadObject[0].addEventListener("change", this._bindSelectFileEvent, false) : uploadObject[0].attachEvent('onChange', this._bindSelectFileEvent);
};
this._bindSelectFileEvent = function (event) {
if( event.target.files.length > 1 ){
alert('只支单视频上传');
return false;
}
var file = event.target.files[0];
if(file.filename == '' || file.filesize == '') {
alert('视频上传错误');
return false;
}
if( file.type.indexOf('video') == -1) {
alert('请上传视频格式文件');
return false;
}
$.getJSON(siteroot + '/share/ajax.html?action=getVideoPlayAuth&inajax=1',{filename:file.name,filesize:file.size}, function (resp) {
console.log(resp);
if(resp.s<0) {
alert(resp.message);
return false;
}
uploadObject.authData = resp.data;
console.log(resp.data);
var uploader = new AliyunUpload.Vod({
//分片大小默认1M,不能小于100K
partSize: 1048576,
//并行上传分片个数,默认5
parallel: 5,
//网络原因失败时,重新上传次数,默认为3
retryCount: 3,
//网络原因失败时,重新上传间隔时间,默认为2秒
retryDuration: 2,
userId : '',
// 开始上传
'onUploadstarted': function (uploadInfo) {
console.log("onUploadStarted:" + uploadInfo.file.name)
uploader.setUploadAuthAndAddress(uploadInfo, uploadObject.authData.UploadAuth, uploadObject.authData.UploadAddress,uploadObject.authData.VideoId);
},
// 文件上传成功
'onUploadSucceed': function (uploadInfo) {
if( $.isFunction(uploadObject._uploadedCallBack) ){
uploadInfo.VideoId = uploadObject.authData.VideoId;
uploadObject._uploadedCallBack(uploadInfo);
}
},
// 文件上传失败
'onUploadFailed': function (uploadInfo, code, message) {
if( $.isFunction(uploadObject._errorCallBack) ){
uploadObject._errorCallBack(uploadInfo, code, message);
}
},
// 文件上传进度,单位:字节
'onUploadProgress': function (uploadInfo, totalSize, uploadedSize) {
if( $.isFunction(uploadObject._progressCallBack) ){
uploadObject._progressCallBack(uploadInfo, totalSize, uploadedSize);
}
},
// 上传凭证超时
'onUploadTokenExpired': function () {
uploader.resumeUploadWithAuth(uploadObject.authData.UploadAuth);
}
});
var userData = {
Vod: {
UserData: {
IsShowWaterMark: false,
Priority: 7
}
}
};
uploader.addFile(file, null, null, null, JSON.stringify(userData));
/*uploader.addFile(file, null, null, null, uploadObject.userData);*/
//开始上传之前执行某个操作
if( $.isFunction(uploadObject._uploadBeforeCallBack) ){
uploadObject._uploadBeforeCallBack();
}
uploader.startUpload();
});
return this;
};
return this;
}
});
})(jQuery);
三、调用
$("#upload_video"+divNums).uploadVideo().setProgress(function(file, totalSize, uploadedSize){//设置进度条 var jindu = Math.ceil(uploadedSize * 100) document.write(jindu+'%'); }).setUploaded(function(file){//上传后操作,要等待转码,所以增加3秒延迟,或者判断是否取得结果 $("input[name='newdata[video_"+divNums+"]").val(file.VideoId); videoId = file.VideoId document.write('视频正在加载中请稍后'); setTimeout(function () { getVideoInfo(videoId,divNums) },3000) }).setError(function (err) { console.log(err) }) .run();