// 文件上传 jquery(function () { var $ = jquery; function getbaseurl() { var scripts = $('script'), elem = scripts[scripts.length - 1], path = elem.src, segments = path.split('/'); segments.pop(); return segments.join('/'); }; // 分片上传配置。 var powerchunkconfig = { /*是否开启分片*/ chunked: true // [默认值:false]是否要分片处理大文件上传 , chunksize: 5 * 1024 * 1024 // [默认值:5242880] 如果要分片,分多大一片? 默认大小为5m。分片大小2m-10m之间, , mergeurl: '/uploadfile/merge' // 合并分片的后端路径。 , inituploaderglobalregistration: function () { if (window.uploaderglobalregistrationed == true) { return; } webuploader.uploader.register({ 'before-send-file': 'beforesendfile', 'before-send': 'beforesend' }, { beforesendfile: function (file) { // 在文件发送之前request,此时还没有分片(如果配置了分片的话),可以用来做文件整体md5验证。 var me = this; onbeforesendfile(file, me); }, beforesend: function (block) { // 在分片发送之前request,可以用来做分片验证,如果此分片已经上传成功了,可返回一个rejected promise来跳过此分片上传。 } }); function onbeforesendfile(file, me) { var owner = me.owner, deferred = webuploader.deferred(); if (!file.identifier) { file.identifier = webuploader.base.guid().replace('wu_', ''); } return deferred.promise(); } window.uploaderglobalregistrationed = true; } , onuploadbeforesend: function (block, data, options) { if (data == null) { data = {}; } // block为分块数据。 // block.file为分块对应的file对象。 // data为要发送的数据 var currentfile = block.file; data.filename = currentfile.name; data.identifier = currentfile.identifier; data.index = block.chunk; data.ischunked = block.chunks > 1; data.chunksize = options.chunksize; currentfile.ischunked = data.ischunked; } /*发送合并文件的请求。*/ , sendmergefiles: function (fileobject, options, fn) { if (fileobject.ischunked && options.mergeurl) { var mergedata = { filename: fileobject.name, identifier: fileobject.identifier, }; var postdata = $.extend(mergedata, options.formdata); // 使用$.ajaxpreventcsrf的时候,重复的属性会导致csrf请求错误。 delete postdata.__requestverificationtoken; var trycount = 0; var retrylimit = 3; $.ajaxpreventcsrf( { url: options.mergeurl, data: postdata, type: 'post', success: function (result) { var jsonresult = powerchunkconfig.tryparsejson(result); fn && fn(fileobject, jsonresult); }, error: function (xhr) { if (powerchunkconfig.checkenableretrystatus(xhr.status)) { trycount++; if (trycount <= retrylimit) { $.ajaxpreventcsrf(this); return; } return; } } }); } else { fn && fn(fileobject); } }, checkenableretrystatus: function (status) { // xhr.status ==504 return status > 399 && status < 599 && status != 403; }, tryparsejson: function (value) { try { if ((typeof (value) == 'string')) { return json.parse(value); } } catch (e) { } return value; } }; powerchunkconfig.inituploaderglobalregistration(); $('[data-ui-type="attachmentuploader"]') .each(function () { var that = $(this); createwebupload(that); }); function createwebupload($attachment) { // 修改的初始化 var attachmentuploader = new attachmentuploader($attachment); return attachmentuploader; } function attachmentuploader($attachment) { var self = this; self.$attachment = $attachment; self.$picker = $attachment.find('#picker'); self.$editvalue = $attachment.find('#editvalue'); self.$filetitle = $attachment.find('#filetitle'); self.$filesize = $attachment.find('#filesize'); self.$fileintro = $attachment.find('#fileintro'); self.$fileurl = $attachment.find('#fileurl'); self.$list = $attachment.find('#thelist'); self.$fileurlerror = $attachment.find('#fileurl-error'); self.$btndelete = $attachment.find('#uploader'); var acceptmimetypes; if ($attachment.length) { var mimetypes = $attachment.attr('data-extensions').split(','); for (var i = 0; i < mimetypes.length; i++) { mimetypes[i] = '.' + mimetypes[i]; } acceptmimetypes = mimetypes.join(','); } self.uploader = webuploader.create({ // 不压缩image resize: false, // swf文件路径 swf: getbaseurl() + '/uploader.swf', // 文件接收服务端。 server: $attachment.attr('data-uploadurl'), // 选择文件的按钮。可选。 // 内部根据当前运行是创建,可能是input元素,也可能是flash. pick: { id: self.$picker, multiple: $attachment.attr('data-isuploadmulti') }, // 拖拽上传。功能可用,样式不对。 dnd: '.' + $attachment.attr('data-dndclass'), // 只允许选择文件,可选。 accept: { title: 'files', extensions: self.$attachment.attr('data-extensions'), // 用户能上传的的类型,不在这范围内的,即使选择了也不会上传。 mimetypes: acceptmimetypes // 点“选择文件”,默认打开该类型的文件。 }, filenumlimit: $attachment.attr('data-filenumlimit'), filesizelimit: $attachment.attr('data-filesizelimit'), filesinglesizelimit: $attachment.attr('data-filesinglesizelimit'), // 缩略图 thumb: { width: 200, height: 200, // 图片质量,只有type为`image/jpeg`的时候才有效。 quality: 100, // 是否允许放大,如果想要生成小图的时候不失真,此选项应该设置为false. allowmagnify: false, // 是否允许裁剪。 crop: true }, // 自动上传。 auto: true, // 文件上传请求的参数表,每次发送都会发送此对象中的参数。 formdata: { uploadproviderkey: $attachment.attr('data-uploadproviderkey'), __requestverificationtoken: pe.security.csrf.getrequestverificationtokenvalue() }, compress: false, chunked: powerchunkconfig.chunked || false, chunksize: powerchunkconfig.chunksize, mergeurl: $attachment.attr('data-mergeurl') || powerchunkconfig.mergeurl }); var model = $attachment.attr('data-editvalue'); if (model) { self.$list.empty(); if (self.$editvalue.attr('data-fileurl')) { self.$list.append('
' + '

' + self.$editvalue.attr('data-filename') + '

' + '

已上传' + ' ' + '

' + '
'); } self.$filetitle.val(self.$editvalue.attr('data-filetitle')); self.$filesize.val(self.$editvalue.attr('data-filesize')); self.$fileintro.val(self.$editvalue.attr('data-fileintro')); self.$fileurl.val(self.$editvalue.attr('data-fileurl')); } self.$btndelete.on('click', "#btndelete", function () { var fileid = $(this).parent().parent().attr("id"); if (fileid) { self.uploader.removefile(fileid, true); } $(this).parent().parent().remove(); self.$filetitle.val(''); self.$fileurl.val(''); }); self.uploader.on('uploadsuccess', function (file, response) { powerchunkconfig.sendmergefiles(file, self.uploader.options, function (newfile, newresponse) { if (newresponse) { response = newresponse; } if (response.iserror) { new pe.ui.message('上传失败', 'error'); return; }; self.$list.empty(); self.$list.append('
' + '

' + response.filename + '

' + '

等待上传...

' + '
'); self.$attachment.find('#' + file.id).find('p.state') .html('已上传' + ' ' + ''); self.$filetitle.val(response.filename); self.$filesize.val(file.size); self.$fileurl.val(response.relativepath); }); }); // 文件上传过程中创建进度条实时显示。 self.uploader.on('uploadprogress', function (file, percentage) { var $li = self.$attachment.find('#' + file.id), $percent = $li.find('.progress .progress-bar'); // 避免重复创建 if (!$percent.length) { $percent = $('
' + '
' + '
' + '
') .appendto($li) .find('.progress-bar'); } $li.find('p.state').text('上传中'); $percent.css('width', percentage * 100 + '%'); }); self.uploader.on('uploaderror', function (file) { self.$attachment.find('#' + file.id).find('p.state').text('上传出错'); }); self.uploader.on('uploadcomplete', function (file) { self.$attachment.find('#' + file.id).find('.progress').fadeout(); }); self.uploader.on('error', function (type, tip) { switch (type) { case 'f_duplicate': alert('您选择的文件已存在,请选择其他文件。'); break; case 'f_exceed_size': alert('单个文件过大。'); break; case 'q_exceed_size_limit': alert('超过总的上传大小。'); break; default: alert(tip); break; } }); self.uploader.on('uploadbeforesend', function (block, data) { powerchunkconfig.onuploadbeforesend(block, data, this.options); }); return self.uploader; } });