(function ($) { /* 获得当前文件路径 */ var js = document.scripts; var path = js[js.length - 1].src.substring(0, js[js.length - 1].src.lastindexof("/") + 1); /* 提交参数 */ var id = null, uploader = null, cansubmit = null, url = null; /* 加载css */ $('') .attr('href', path + 'css/css.css') .attr('rel', 'stylesheet') .appendto('head'); /* 加载上传js */ $('') .attr('src', path + 'js/webuploader.js') .appendto('body'); var html = ''; html = '
' + '
' + '
上传简历
' + '
' + '
' + '
' + '
未选择文件
' + '
请选择word文件上传
' + '' + '
'; function createlayer() { if ($('.hxq_upload').length == 0) { $(html).appendto('body'); if (document.body.draggable !== undefined) { $('[upload-select-message]').html('请将文件拖至虚线框内或者点击选择文件'); } else { $('[upload-select-message]').html('您的浏览器不支持拖拽上传,请点击虚线框选择文件!'); } uploadinit(); } } function destorylayer() { $('.hxq_upload').remove(); } $.fn.cvupload = function (opts) { id = opts.id; url = opts.url; cansubmit = opts.cansubmit; /* 遍历按钮添加点击事件 */ this.each(function () { $(this).on('click', function () { id = $(this).attr('cvupload'); createlayer(); }); }); } /* 关闭弹窗 */ $(document).on('click', '[upload-box-close]', function () { destorylayer(); }); function create() { uploader.addbutton({ id: '#filepicker', innerhtml: '上传' }); } function uploadinit() { uploader = webuploader.create({ // swf文件路径 swf: path + 'js/uploader.swf', // 文件接收服务端。 server: url, // 选择文件的按钮。可选。 // 内部根据当前运行是创建,可能是input元素,也可能是flash. pick: { id: $('[upload-select]'), innerhtml: '', multiple: false }, // 不压缩image, 默认如果是jpeg,文件上传前会压缩一把再上传! resize: false, // 开启拖拽上传 disableglobaldnd: false, dnd: $('[upload-select]'), // 上传到服务器的参数 formdata: { jid: id } }); addevent(); } /* 开始上传 */ $(document).on('click', '[upload-submit]', function (e) { var _this = e.target; if (!($(_this).hasclass('disable1') || $(_this).hasclass('disable2'))) { $(_this).html('正在上传...请勿关闭本弹窗'); $(_this).addclass('disable2'); uploader.upload(); } if ($(_this).hasclass('retry')) { uploader.retry(); } }); function addevent() { /* 选择文件时清空提示 */ uploader.on('beforefilequeued', function (file) { uploader.reset(); $('[upload-message]') .show(0) .attr('class', '') .html(''); $('[upload-process]').find('span').html(''); if (file.size == 0) { $('[upload-message]') .addclass('hxq_upload_waring') .html('无法上传空文件!'); } else if (file.size > 5242880) { $('[upload-message]') .addclass('hxq_upload_waring') .html('文件大小不能超过5mb!'); return false; } if (!cansubmit) { $('[upload-message]') .addclass('hxq_upload_waring') .html('静态页无法提交!'); return false; } if (window.console && window.console.log) { console.log(file.size); } }); /* 文件选择完成触发 */ uploader.on('filequeued', function (file) { if (file.ext.tolowercase() == 'doc' || file.ext.tolowercase() == 'docx') { $('[upload-message]').attr('class', ''); $('[upload-submit]') .removeclass('disable1') .html('上传'); $('[upload-filename]').html(cstr(file.name.replace(/\.\w+$/, ''), 18) + '.' + file.ext + '  (' + webuploader.base.formatsize(file.size, ['b', 'kb', 'mb', 'gb']) + ')'); $('[upload-filename]').attr('title', 'file.name'); } else { $('[upload-message]') .addclass('hxq_upload_error') .html('只支持上传word文件!'); } }); /* 文件上传进度 */ uploader.on('uploadprogress', function (file, percentage) { $('[upload-message]').hide(); $('[upload-process]') .show() .find('span') .css('width', parseint(percentage * 100) + '%'); if (parseint(percentage * 100) > 15) { $('[upload-process]').find('span').html(parseint(percentage * 100) + '%'); } }); /* 文件上传出错 */ uploader.on('uploaderror', function (file, reason) { if (window.console && window.console.log) { console.log(reason); } }); /* 文件上传成功 */ uploader.on('uploadsuccess', function (file, response) { if (window.console && window.console.log) { console.log(response); } if (false) { file.setstatus('inited'); $('[upload-message]') .delay(500) .fadein(100) .attr('class', '') .addclass('hxq_upload_error') .html('服务器接收失败,请重新上传'); $('[upload-submit]') .removeclass('disable2') .addclass('retry') .html('重传'); } else { $('[upload-message]') .delay(500).fadein(100) .attr('class', '') .addclass('hxq_upload_success') .html(cstr(file.name.replace(/\.\w+$/, ''), 10) + '.' + file.ext + '  成功上传'); $('[upload-submit]') .removeclass('disable2') .addclass('disable1') .html('上传'); } }); /* 文件上传完成 */ uploader.on('uploadcomplete', function (file) { $('[upload-process]').delay(500).hide(0); }); /* 错误信息 */ uploader.on('error', function (err) { if (window.console && window.console.log) { console.log(err); } }); } /* 截字符 */ function cstr(str, len) { var str_length = 0; var str_len = 0; var a = ''; str_cut = new string(); str_len = str.length; for (var i = 0; i < str_len; i++) { a = str.charat(i); str_length++; if (escape(a).length > 4) { str_length++; } } if (str_length > len + 4) { var end = 0; var start = 0; var start_str = ''; var end_str = ''; for (var i = 0; i < str_len; i++) { a = str.charat(i); start++; if (escape(a).length > 4) { //中文字符的长度经编码之后大于4 start++; } start_str = start_str + a; if (start > len - 5) { break; } } for (var i = str_len; i > 0; i--) { a = str.charat(i); end++; if (escape(a).length > 4) { //中文字符的长度经编码之后大于4 end++; } end_str = a + end_str; if (end > 4) { break; } } return start_str + '...' + end_str; } else { return str; } console.log(str_length); //如果给定字符串小于指定长度,则返回源字符串; /*if (str_length < len) { return str; }*/ } })(jquery);