accessid = ''
accesskey = ''
host = ''
policyBase64 = ''
signature = ''
callbackbody = ''
filename = ''
key = ''
expire = 0
g_object_name = ''
g_object_name_type = ''
now = timestamp = Date.parse(new Date()) / 1000;
function send_request()
{
var xmlhttp = null;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
if (sarry==""){
serverUrl = 'url?'+"order_id="+sid+"&"+"order_number="+snum;//单个
}else{
serverUrl = 'url'+"order_id="+sarry+"&"+"order_number="+snum;
}
//serverUrl = 'url?'+"order_id="+sid+"&"+"order_number="+snum;
xmlhttp.open( "GET", serverUrl, false );
xmlhttp.withCredentials = true;
xmlhttp.send(null);
return xmlhttp.responseText;
}
else
{
layer.msg("Your browser does not support XMLHTTP.",{icon:5});
}
};
function check_object_radio() {
var tt = document.getElementsByName('myradio');
for (var i = 0; i < tt.length ; i++ )
{
if(tt[i].checked)
{
g_object_name_type = tt[i].value;
break;
}
}
}
function get_signature()
{
//可以判断当前expire是否超过了当前时间,如果超过了当前时间,就重新取一下.3s 做为缓冲
now = timestamp = Date.parse(new Date()) / 1000;
if (expire < now + 3)
{
body = send_request()
var obj = eval ("(" + body + ")");
host = obj['host']
policyBase64 = obj['policy']
accessid = obj['accessid']
signature = obj['signature']
expire = parseInt(obj['expire'])
callbackbody = obj['callback']
key = obj['dir']
return true;
}
return false;
};
function random_string(len) {
len = len || 32;
var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
var maxPos = chars.length;
var pwd = '';
for (i = 0; i < len; i++) {
pwd += chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
function get_suffix(filename) {
pos = filename.lastIndexOf('.')
suffix = ''
if (pos != -1) {
suffix = filename.substring(pos)
}
return suffix;
}
function calculate_object_name(filename)
{
if (g_object_name_type == 'local_name')
{
g_object_name += "${filename}"
}
else if (g_object_name_type == 'random_name')
{
suffix = get_suffix(filename)
g_object_name = key + random_string(10) + suffix
}
return ''
}
function get_uploaded_object_name(filename)
{
if (g_object_name_type == 'local_name')
{
tmp_name = g_object_name
tmp_name = tmp_name.replace("${filename}", filename);
return tmp_name
}
else if(g_object_name_type == 'random_name')
{
return g_object_name
}
}
function set_upload_param(up, filename, ret)
{
if (ret == false)
{
ret = get_signature()
}
g_object_name = key;
if (filename != '') { suffix = get_suffix(filename)
calculate_object_name(filename)
}
new_multipart_params = {
'key' : g_object_name,
'policy': policyBase64,
'OSSAccessKeyId': accessid,
'success_action_status' : '200', //让服务端返回200,不然,默认会返回204
'callback' : callbackbody,
'signature': signature,
};
up.setOption({
'url': host,
'multipart_params': new_multipart_params
});
up.start();
}
var uploader = new plupload.Uploader({
runtimes : 'html5,flash,silverlight,html4',
browse_button : 'selectfiles',
//multi_selection: false,
container: document.getElementById('container'),
flash_swf_url : 'lib/plupload-2.1.2/js/Moxie.swf',
silverlight_xap_url : 'lib/plupload-2.1.2/js/Moxie.xap',
url : 'http://oss.aliyuncs.com',
multi_selection: 'false',//只能上传单个
unique_names:true,//生成唯一名
filters: {
mime_types : [
{ title : "Video files", extensions : "mp4,avi" }
],
max_file_size : '120mb', //最大只能上传10mb的文件
prevent_duplicates : false //true不允许选取重复文件
},
init: {
PostInit: function(files) {
document.getElementById('ossfile').innerHTML = '';
document.getElementById('postfiles').onclick = function() {
var input_video = document.getElementById("video");
// if(input_video.duration>120){//需要先判断选中的时常是否相等,取他的时常
if(setime==""){
if(input_video.duration>alltime){//多个上传得到的购买时常
layer.msg("您购买的时长为"+alltime+"s。"+"该视频的时长超过您所购买的投放时间,请重新选择视频",{icon:7,time:2000});
return false;
}
}else{
if(input_video.duration>setime){//单个上传得到的购买时常
layer.msg("您购买的时长为"+setime+"s。"+"该视频的时长超过您所购买的投放时间,请重新选择视频",{icon:7,time:2000});
return false;
}
}
var calc_video = (input_video.videoWidth/input_video.videoHeight).toString();
var videoc =(16/9).toString();
var videok =(4/3).toString();
if(calc_video != videoc && calc_video !=videok){
layer.msg("视频不是16:9的格式或者4/3的格式,不允许上传",{icon:7,time:2000});
return false;
}
set_upload_param(uploader, '', false);
uploader.start();
return false;
};
},
UploadFile:function(uploader,file){//当上传队列中某一个文件开始上传后触发,
},
FilesAdded: function(up, files) {
$.each(up.files, function (i, file) { //单个上传,替换文件
if (up.files.length <= 1) {
document.getElementById('ossfile').innerHTML = '';
return;
}
up.removeFile(file);
});
//有用
var input_video = document.getElementById("video");
var furl =URL.createObjectURL(files[0].getNative());
input_video.src=furl;
plupload.each(files, function(file) {
document.getElementById('ossfile').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ')<b></b>'
+'<div class="progress"><div class="progress-bar" style="width: 0%"></div></div>'
+'</div>';
});
},
BeforeUpload: function(up, file) {
check_object_radio();
set_upload_param(up, file.name, true);
},
UploadProgress: function(up, file) {
var d = document.getElementById(file.id);
d.getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
var prog = d.getElementsByTagName('div')[0];
var progBar = prog.getElementsByTagName('div')[0]
progBar.style.width= 2*file.percent+'px';
progBar.setAttribute('aria-valuenow', file.percent);
},
FileUploaded: function(up, file, info) {
if (info.status == 200)
{
var result = eval('('+info.response+')');
if(result.status=="success"){
layer.msg(result.message,{icon:6,time:2000},function(){
parent.layer.close(index);
var pac = window.parent.document.getElementById('bindclick');
pac.click();
});
}
if(result.status=="fail"){
layer.msg(result.message,{icon:5,time:2000},function(){
parent.layer.close(index);
var pac = window.parent.document.getElementById('bindclick');
pac.click();
})
}
}
else if (info.status == 203)
{
// document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '上传到OSS成功,但是oss访问用户设置的上传回调服务器失败,失败原因是:' + info.response;
}
else
{
// document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = info.response;
}
},
Error: function(up, err) {
if (err.code == -600) {
// document.getElementById('console').appendChild(document.createTextNode("\n选择的文件太大了,可以根据应用情况,在upload.js 设置一下上传的最大大小"));
layer.msg('选择的文件太大了',{icon:7},{time:2000});
return;
}
else if (err.code == -601) {
// document.getElementById('console').appendChild(document.createTextNode("\n选择的文件后缀不对,可以根据应用情况,在upload.js进行设置可允许的上传文件类型"));
layer.msg('请选择正确的视频文件',{icon:7},{time:2000});
return;
}
else if (err.code == -602) {
//document.getElementById('console').appendChild(document.createTextNode("\n这个文件已经上传过一遍了"));
layer.msg('这个文件已经上传过一遍了',{icon:7},{time:2000});
return;
}
else
{
// document.getElementById('console').appendChild(document.createTextNode("\nError xml:" + err.response));
layer.msg(err.response,{icon:7},{time:2000});
return;
}
}
}
});
uploader.init();
附:gif效果 http://www.cnblogs.com/zhixiaoxiao/gallery/image/200811.html