uploader.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. $(function () {
  2. //初始化绑定默认的属性
  3. $.upLoadDefaults = $.upLoadDefaults || {};
  4. $.upLoadDefaults.property = {
  5. multiple: false, //是否多文件
  6. water: false, //是否加水印
  7. thumbnail: false, //是否生成缩略图
  8. sendurl: null, //发送地址
  9. filetypes: "jpg,jpeg,png,gif", //文件类型
  10. filesize: "2048", //文件大小
  11. btntext: "浏览...", //上传按钮的文字
  12. swf: null, //SWF上传控件相对地址
  13. fileType:100 // 10开头是图片 -图片 20开头是文件
  14. };
  15. //初始化上传控件
  16. $.fn.InitUploader = function (b) {
  17. var fun = function (parentObj) {
  18. var p = $.extend({}, $.upLoadDefaults.property, b || {});
  19. var btnObj = $('<div class="upload-btn btn-success"><i class="fa fa-plus"></i>&nbsp;' + p.btntext + '</div>').appendTo(parentObj);
  20. //初始化属性.appendTo(parentObj)
  21. //p.sendurl += "?action=UpLoadFile";
  22. // if (p.water) {
  23. // p.sendurl += "&IsWater=1";
  24. //}
  25. //if (p.thumbnail) {
  26. // p.sendurl += "&IsThumbnail=1";
  27. //}
  28. //if (!p.multiple) {
  29. // p.sendurl += "&DelFilePath=" + parentObj.siblings(".upload-path").val();
  30. //}
  31. if(p.sendurl == null || typeof(p.sendurl) == 'undefined'){
  32. var file_type = parseInt(p.fileType);
  33. if(file_type >= 100 && file_type < 200){
  34. p.sendurl = config.pic_upload_path;
  35. }
  36. else if(file_type >= 200){
  37. p.sendurl = config.file_upload_path;
  38. }
  39. }
  40. //初始化WebUploader
  41. var uploader = WebUploader.create({
  42. auto: true, //自动上传
  43. swf: p.swf, //SWF路径
  44. server: p.sendurl, //上传地址
  45. pick: {
  46. id: btnObj,
  47. multiple: p.multiple
  48. },
  49. accept: {
  50. /*title: 'Images',*/
  51. // extensions: p.filetypes
  52. /*mimeTypes: 'image/*'*/
  53. },
  54. formData: {
  55. 'DelFilePath': '', //定义参数
  56. 'fileType':p.fileType
  57. },
  58. fileVal: 'Filedata', //上传域的名称
  59. fileSingleSizeLimit: p.filesize * 1024 //文件大小
  60. });
  61. //当validate不通过时,会以派送错误事件的形式通知
  62. uploader.on('error', function (type) {
  63. switch (type) {
  64. case 'Q_EXCEED_NUM_LIMIT':
  65. alert("错误:上传文件数量过多!");
  66. break;
  67. case 'Q_EXCEED_SIZE_LIMIT':
  68. alert("错误:文件总大小超出限制!");
  69. break;
  70. case 'F_EXCEED_SIZE':
  71. alert("错误:文件大小超出限制!");
  72. break;
  73. case 'Q_TYPE_DENIED':
  74. alert("错误:禁止上传该类型文件!");
  75. break;
  76. case 'F_DUPLICATE':
  77. alert("错误:请勿重复上传该文件!");
  78. break;
  79. default:
  80. alert('错误代码:' + type);
  81. break;
  82. }
  83. });
  84. //当有文件添加进来的时候
  85. uploader.on('fileQueued', function (file) {
  86. //如果是单文件上传,把旧的文件地址传过去
  87. if (!p.multiple) {
  88. uploader.options.formData.DelFilePath = parentObj.siblings(".upload-path").val();
  89. uploader.options.formData.fileType = p.fileType;
  90. }
  91. //防止重复创建
  92. if (parentObj.children(".upload-progress").length == 0) {
  93. //创建进度条
  94. var fileProgressObj = $('<div class="upload-progress"></div>').appendTo(parentObj);
  95. var progressText = $('<span class="txt">正在上传,请稍候...</span>').appendTo(fileProgressObj);
  96. var progressBar = $('<span class="bar"><b></b></span>').appendTo(fileProgressObj);
  97. var progressCancel = $('<a class="close" title="取消上传">关闭</a>').appendTo(fileProgressObj);
  98. //绑定点击事件
  99. progressCancel.click(function () {
  100. uploader.cancelFile(file);
  101. fileProgressObj.remove();
  102. });
  103. }
  104. });
  105. //文件上传过程中创建进度条实时显示
  106. uploader.on('uploadProgress', function (file, percentage) {
  107. var progressObj = parentObj.children(".upload-progress");
  108. progressObj.children(".txt").html(file.name);
  109. progressObj.find(".bar b").width(percentage * 100 + "%");
  110. });
  111. //当文件上传出错时触发
  112. uploader.on('uploadError', function (file, reason) {
  113. uploader.removeFile(file); //从队列中移除
  114. alert(file.name + "上传失败,错误代码:" + reason);
  115. });
  116. //当文件上传成功时触发
  117. uploader.on('uploadSuccess', function (file, data) {
  118. if (data.status == '0') {
  119. var progressObj = parentObj.children(".upload-progress");
  120. progressObj.children(".txt").html(data.msg);
  121. }
  122. if (data.status == '1') {
  123. // 文件
  124. if(p.fileType >= 100 && p.fileType < 200){
  125. //如果是单文件上传,则赋值相应的表单
  126. if (!p.multiple) {
  127. /*parentObj.siblings(".upload-name").val(file.name);
  128. parentObj.siblings(".upload-path").val(data.path);
  129. parentObj.siblings(".upload-size").val(file.size);*/
  130. // parentObj.after(parentObj.prev().clone(true).attr('value', data.path));
  131. //addImage(parentObj, data.path, data.thumb,data.rootPath);
  132. addSingleImage(parentObj, data.path, data.thumb,data.rootPath);
  133. } else {
  134. addImage(parentObj, data.path, data.thumb,data.rootPath);
  135. }
  136. }
  137. else if(p.fileType >= 200){
  138. if (!p.multiple) {
  139. parentObj.siblings(".upload-name").val(file.name);
  140. parentObj.siblings(".upload-path").val(data.path);
  141. parentObj.siblings(".upload-size").val(file.size);
  142. $(parentObj).prevAll('.show-file').html(file.name);
  143. // $(targetObj).prevAll('.show-file').attr('text', rootPath + thumbSrc );
  144. }
  145. }
  146. else {
  147. return ;
  148. }
  149. var progressObj = parentObj.children(".upload-progress");
  150. progressObj.children(".txt").html("上传成功:" + file.name);
  151. }
  152. uploader.removeFile(file); //从队列中移除
  153. });
  154. //不管成功或者失败,文件上传完成时触发
  155. uploader.on('uploadComplete', function (file) {
  156. var progressObj = parentObj.children(".upload-progress");
  157. progressObj.children(".txt").html("上传完成");
  158. //如果队列为空,则移除进度条
  159. if (uploader.getStats().queueNum == 0) {
  160. progressObj.remove();
  161. }
  162. });
  163. };
  164. return $(this).each(function () {
  165. fun($(this));
  166. });
  167. }
  168. });
  169. function addSingleImage(targetObj, originalSrc, thumbSrc, rootPath){
  170. //var newLi = $('<img style="height:112px; width:112px;" src="' +rootPath + thumbSrc + '" />');
  171. // $(targetObj).pre
  172. //newLi.appendTo(targetObj);
  173. // $(targetObj).prevAll('img').attr('src', rootPath + thumbSrc );
  174. var newInput = targetObj.prev().clone(true).attr('value', originalSrc);
  175. console.info(newInput);
  176. $(targetObj).prev().prev().before('<div class="img-box" style="float:left"><img style="height:100px; width:100px;" src="' +rootPath + thumbSrc + '" />' + newInput[0].outerHTML + '<a href="javascript:;" onclick="delImg(this);">删除</a></div>');
  177. }
  178. /*图片相册处理事件
  179. =====================================================*/
  180. //添加图片相册
  181. function addImage(targetObj, originalSrc, thumbSrc,rootPath) {
  182. //插入到相册UL里面
  183. var newLi = $('<li>'
  184. + '<input type="hidden" name="hid_photo_name" value="0|' + originalSrc + '|' + thumbSrc + '" />'
  185. + '<input type="hidden" name="hid_photo_remark" value="" />'
  186. + '<div class="img-box" onclick="setFocusImg(this);">'
  187. + '<img src="' +rootPath+ thumbSrc + '" bigsrc="' +rootPath+ originalSrc + '" />'
  188. // + '<span class="remark"><i>暂无描述...</i></span>'
  189. + '</div>'
  190. //+ '<a href="javascript:;" onclick="setRemark(this);">描述</a>'
  191. + '<a href="javascript:;" onclick="delImg(this);">删除</a>'
  192. + '</li>');
  193. newLi.appendTo(targetObj.siblings(".photo-list").children("ul"));
  194. //默认第一个为相册封面
  195. var focusPhotoObj = targetObj.siblings(".focus-photo");
  196. if (focusPhotoObj.val() == "") {
  197. focusPhotoObj.val(thumbSrc);
  198. newLi.children(".img-box").addClass("selected");
  199. }
  200. }
  201. //设置相册封面
  202. function setFocusImg(obj) {
  203. var focusPhotoObj = $(obj).parents(".photo-list").siblings(".focus-photo");
  204. focusPhotoObj.val($(obj).children("img").eq(0).attr("src"));
  205. $(obj).parent().siblings().children(".img-box").removeClass("selected");
  206. $(obj).addClass("selected");
  207. }
  208. //设置图片描述
  209. function setRemark(obj) {
  210. var parentObj = $(obj); //父对象
  211. var hidRemarkObj = parentObj.prevAll("input[name='hid_photo_remark']").eq(0); //取得隐藏值
  212. var d = parent.dialog({
  213. title: "图片描述",
  214. content: '<textarea id="ImageRemark" style="margin:10px 0;font-size:12px;padding:3px;color:#000;border:1px #d2d2d2 solid;vertical-align:middle;width:300px;height:50px;">' + hidRemarkObj.val() + '</textarea>',
  215. button: [{
  216. value: '批量描述',
  217. callback: function () {
  218. var remarkObj = $('#ImageRemark', parent.document);
  219. if (remarkObj.val() == "") {
  220. parent.dialog({
  221. title: '提示',
  222. content: '亲,总该写点什么吧?',
  223. okValue: '确定',
  224. ok: function () { },
  225. onclose: function(){
  226. remarkObj.focus();
  227. }
  228. }).showModal();
  229. return false;
  230. }
  231. parentObj.parent().parent().find("li input[name='hid_photo_remark']").val(remarkObj.val());
  232. parentObj.parent().parent().find("li .img-box .remark i").html(remarkObj.val());
  233. }
  234. }, {
  235. value: '单张描述',
  236. callback: function () {
  237. var remarkObj = $('#ImageRemark', parent.document);
  238. if (remarkObj.val() == "") {
  239. parent.dialog({
  240. title: '提示',
  241. content: '亲,总该写点什么吧?',
  242. okValue: '确定',
  243. ok: function () { },
  244. onclose: function () {
  245. remarkObj.focus();
  246. }
  247. }).showModal();
  248. return false;
  249. }
  250. hidRemarkObj.val(remarkObj.val());
  251. parentObj.siblings(".img-box").children(".remark").children("i").html(remarkObj.val());
  252. },
  253. autofocus: true
  254. }]
  255. }).showModal();
  256. }
  257. function delImg(obj) {
  258. $(obj).parent().remove(); //删除的LI节点
  259. }
  260. //删除图片LI节点
  261. /*
  262. function delImg(obj) {
  263. var parentObj = $(obj).parent().parent();
  264. var focusPhotoObj = parentObj.parent().siblings(".focus-photo");
  265. var smallImg = $(obj).siblings(".img-box").children("img").attr("src");
  266. $(obj).parent().remove(); //删除的LI节点
  267. //检查是否为封面
  268. if (focusPhotoObj.val() == smallImg) {
  269. focusPhotoObj.val("");
  270. var firtImgBox = parentObj.find("li .img-box").eq(0); //取第一张做为封面
  271. firtImgBox.addClass("selected");
  272. focusPhotoObj.val(firtImgBox.children("img").attr("src")); //重新给封面的隐藏域赋值
  273. }
  274. }*/