webUploadUtil.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /**
  2. * 对百度WebUploader的封装
  3. * 因为这里考虑到每个需求可能不一样,比如文件大小限制,文件格式限制,宽高等,还是没法做成共通的插件
  4. * 所以这些基本参数需要调用者自己定义,这里对回调进行了封装,调用者无需关心处理回调
  5. */
  6. (function () {
  7. // 判断ie有没有安装flash播放器
  8. function IEVersion() {
  9. var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
  10. var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE浏览器
  11. if (isIE) {
  12. var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
  13. reIE.test(userAgent);
  14. var fIEVersion = parseFloat(RegExp["$1"]);
  15. if (fIEVersion == 7 || fIEVersion == 8 || fIEVersion == 9) {
  16. var hasFlash = false;
  17. try {
  18. hasFlash = Boolean(new ActiveXObject('ShockwaveFlash.ShockwaveFlash'));
  19. } catch (exception) {
  20. hasFlash = ('undefined' != typeof navigator.mimeTypes['application/x-shockwave-flash']);
  21. var r = confirm("您的浏览器不支持上传文件,请安装flash播放器");
  22. if (r == true) {
  23. window.open("https://get.adobe.com/tw/flashplayer/");
  24. }
  25. else {
  26. }
  27. }
  28. }
  29. }
  30. };
  31. /**
  32. * 图片
  33. * @param options
  34. */
  35. var $WebUploadPicture = function (options) {
  36. this.auto = options.auto || true; //开启自动上传
  37. this.uploadBtnId = options.uploadBtnId || '#filePicker'; //选择图片按钮,id|class自己定义
  38. this.uploadPreId = options.picturePreId || 'fileListPre'; //图片预览ID
  39. this.uploadUrl = options.serverUrl || pagePath + '/upload/images'; //上传URL
  40. this.swf = pagePath + '/h-ui/lib/webuploader/0.1.5/Uploader.swf';
  41. this.pictureUrl = options.hiddenPictureUrl || 'fileDown.file_id'; //隐藏域name,用于往后台传值
  42. this.picWidth = options.width; //图片宽度
  43. this.picHeight = options.height; //图片高度
  44. this.fileSizeLimit = 300 * 1024 * 1024; //验证文件总大小是否超出限制, 超出则不允许加入队列
  45. this.fileSingleSizeLimit = 300 * 1024 * 1024; // 验证单个文件大小是否超出限制, 超出则不允许加入队列
  46. //this.fileSizeLimit = options.fileSizeLimit || 100 * 1024 * 1024; //验证文件总大小是否超出限制, 超出则不允许加入队列
  47. //this.fileSingleSizeLimit = options.fileSingleSizeLimit || 100 * 1024 * 1024; // 验证单个文件大小是否超出限制, 超出则不允许加入队列
  48. this.fileNumLimit = options.fileNumLimit || undefined; // 验证文件总数量, 超出则不允许加入队列
  49. this.accept = options.accept; //指定接受哪些类型的文件
  50. this.append = options.append || false; //是否开启追加多个图片
  51. this.uploadBarId = null; //图片上传的进度条的id
  52. };
  53. var
  54. // 优化retina, 在retina下这个值是2
  55. ratio = window.devicePixelRatio || 1,
  56. // 缩略图大小
  57. thumbnailWidth = 100 * ratio,
  58. thumbnailHeight = 100 * ratio;
  59. $WebUploadPicture.prototype = {
  60. /**
  61. * 初始化webUploader
  62. */
  63. init: function () {
  64. // 判断ie有没有安装flash播放器
  65. IEVersion();
  66. var uploader = this.create();
  67. this.bindEvent(uploader);
  68. return uploader;
  69. },
  70. /**
  71. * 创建webuploader对象
  72. */
  73. create: function () {
  74. return WebUploader.create({
  75. auto: this.auto,
  76. pick: {
  77. id: this.uploadBtnId,
  78. multiple: true // 只上传一个
  79. },
  80. accept: this.accept,
  81. swf: this.swf,
  82. disableGlobalDnd: true,
  83. duplicate: true,
  84. server: this.uploadUrl,
  85. fileSizeLimit: this.fileSizeLimit,
  86. fileSingleSizeLimit: this.fileSingleSizeLimit,
  87. fileNumLimit: this.fileNumLimit
  88. });
  89. },
  90. /**
  91. * 绑定事件
  92. */
  93. bindEvent: function (bindObj) {
  94. var me = this;
  95. //当文件被加入队列以后触发
  96. //当文件被加入队列以后触发
  97. bindObj.on('fileQueued', function (file) {
  98. var $li = $(
  99. '<div id="' + file.id + '" class="file-item thumbnail">' +
  100. '<img width="' + me.picWidth + '" height="' + me.picHeight + '">' +
  101. '<input type="hidden" name="' + me.pictureUrl + '" id="' + file.id + 'PicUrl" />' +
  102. '<a href="javascript:;" onclick="delDoc(this);">删除</a>' +
  103. '<div class="info">' + file.name + '</div>' +
  104. '</div>'
  105. );
  106. // var $li = $('<div><img width="100px" height="100px"></div>');
  107. var $img = $li.find('img');
  108. if (me.append) {
  109. $("#" + me.uploadPreId).append($li);
  110. } else {
  111. $("#" + me.uploadPreId).html($li);
  112. }
  113. // 生成缩略图
  114. bindObj.makeThumb(file, function (error, src) {
  115. if (error) {
  116. $img.replaceWith('<span>不能预览</span>');
  117. return;
  118. }
  119. $img.attr('src', src);
  120. }, thumbnailWidth, thumbnailHeight);
  121. });
  122. // 文件上传过程中创建进度条实时显示。
  123. bindObj.on('uploadProgress', function (file, percentage) {
  124. // $("#"+me.uploadBarId).css("width",percentage * 100 + "%");
  125. var $li = $('#' + file.id),
  126. $percent = $li.find('.progress span');
  127. // 避免重复创建
  128. if (!$percent.length) {
  129. $percent = $('<p class="progress"><span></span></p>')
  130. .appendTo($li)
  131. .find('span');
  132. }
  133. $percent.css('width', percentage * 100 + '%');
  134. });
  135. // 文件上传成功,给item添加成功class, 用样式标记上传成功。
  136. bindObj.on('uploadSuccess', function (file, response) {
  137. succeedMessage("上传成功");
  138. //隐藏域ID,用于往后台传值
  139. $("#" + file.id + "PicUrl").val(response);
  140. $('#' + file.id).addClass('upload-state-done');
  141. picSuccess = true;
  142. });
  143. // 完成上传完了,成功或者失败,先删除进度条。
  144. bindObj.on('uploadComplete', function (file) {
  145. $('#' + file.id).find('.progress').remove();
  146. });
  147. // 文件上传失败,显示上传出错。
  148. bindObj.on('uploadError', function (file, response) {
  149. //errorMessage(response);
  150. errorMessage('上传出错');
  151. var $li = $('#' + file.id),
  152. $error = $li.find('div.error');
  153. // 避免重复创建
  154. if (!$error.length) {
  155. $error = $('<div class="error"></div>').appendTo($li);
  156. }
  157. $error.text('上传失败');
  158. //移除上传失败的隐藏域input值
  159. $('#' + file.id + 'PicUrl').remove();
  160. });
  161. // 其他错误
  162. bindObj.on('error', function (type) {
  163. if ("Q_EXCEED_SIZE_LIMIT" == type) {
  164. errorMessage("文件大小超出了限制");
  165. } else if ("Q_TYPE_DENIED" == type) {
  166. errorMessage("文件类型不满足");
  167. } else if ("Q_EXCEED_NUM_LIMIT" == type) {
  168. errorMessage("上传数量超过限制");
  169. } else if ("F_DUPLICATE" == type) {
  170. errorMessage("图片选择重复");
  171. } else if ("F_EXCEED_SIZE" == type) {
  172. errorMessage("文件大小超出了限制");
  173. } else {
  174. errorMessage("上传过程中出错");
  175. }
  176. });
  177. },
  178. /**
  179. * 设置图片上传的进度条的id
  180. */
  181. setUploadBarId: function (id) {
  182. this.uploadBarId = id;
  183. }
  184. };
  185. /**
  186. * 文件
  187. * @param options
  188. */
  189. var $WebUploadDoc = function (options) {
  190. this.auto = options.auto || true; //开启自动上传
  191. this.uploadBtnId = options.uploadBtnId || '#filePicker'; //选择上传按钮,id|class自己定义
  192. this.uploadPreId = options.docPreId || 'fileListPre'; //预览ID
  193. this.uploadUrl = options.serverUrl || pagePath + '/upload/docs'; //上传URL
  194. this.swf = pagePath + '/h-ui/lib/webuploader/0.1.5/Uploader.swf';
  195. this.docUrl = options.hiddenDocUrl || 'fileDown.file_id'; //隐藏域name,用于往后台传值
  196. this.fileSizeLimit = 1024 * 1024 * 1024 * 5; //验证文件总大小是否超出限制, 超出则不允许加入队列
  197. this.fileSingleSizeLimit = 1024 * 1024 * 1024 * 5; // 验证单个文件大小是否超出限制, 超出则不允许加入队列
  198. //this.fileSizeLimit = options.fileSizeLimit || 100 * 1024 * 1024; //验证文件总大小是否超出限制, 超出则不允许加入队列
  199. //this.fileSingleSizeLimit = options.fileSingleSizeLimit || 2 * 1024 * 1024; // 验证单个文件大小是否超出限制, 超出则不允许加入队列
  200. this.fileNumLimit = options.fileNumLimit || undefined; // 验证文件总数量, 超出则不允许加入队列
  201. this.accept = options.accept; //指定接受哪些类型的文件
  202. this.append = options.append || false; //是否开启追加多个
  203. this.uploadBarId = null; //上传的进度条的id
  204. };
  205. $WebUploadDoc.prototype = {
  206. /**
  207. * 初始化webUploader
  208. */
  209. init: function () {
  210. IEVersion();
  211. var uploader = this.create();
  212. this.bindEvent(uploader);
  213. return uploader;
  214. },
  215. /**
  216. * 创建webuploader对象
  217. */
  218. create: function () {
  219. return WebUploader.create({
  220. auto: this.auto,
  221. pick: {
  222. id: this.uploadBtnId,
  223. multiple: false // 只上传一个
  224. },
  225. accept: this.accept,
  226. swf: this.swf,
  227. disableGlobalDnd: true,
  228. duplicate: true,
  229. server: this.uploadUrl,
  230. fileSizeLimit: this.fileSizeLimit,
  231. fileSingleSizeLimit: this.fileSingleSizeLimit,
  232. fileNumLimit: this.fileNumLimit
  233. });
  234. },
  235. /**
  236. * 绑定事件
  237. */
  238. bindEvent: function (bindObj) {
  239. var me = this;
  240. //当文件被加入队列以后触发
  241. bindObj.on('fileQueued', function (file) {
  242. var $li = $(
  243. '<div id="' + file.id + '" class="item">' +
  244. '<h4 class="info">' + file.name + '</h4>' +
  245. '<input type="hidden" name="' + me.docUrl + '" id="' + file.id + 'DocUrl" />' +
  246. '<a href="javascript:;" onclick="delDoc(this);">删除</a>' +
  247. '<p class="state">等待上传...</p>' +
  248. '</div>'
  249. );
  250. if (me.append) {
  251. $("#" + me.uploadPreId).append($li);
  252. } else {
  253. $("#" + me.uploadPreId).html($li);
  254. }
  255. });
  256. // 文件上传过程中创建进度条实时显示。
  257. bindObj.on('uploadProgress', function (file, percentage) {
  258. // $("#"+me.uploadBarId).css("width",percentage * 100 + "%");
  259. var $li = $('#' + file.id),
  260. $percent = $li.find('.progress .progress-bar');
  261. // 避免重复创建
  262. if (!$percent.length) {
  263. $percent = $('<div class="progress progress-striped active">' +
  264. '<div class="progress-bar" role="progressbar" style="width: 0%">' +
  265. '</div>' +
  266. '</div>').appendTo($li).find('.progress-bar');
  267. }
  268. $li.find('p.state').text('上传中');
  269. $percent.css('width', percentage * 100 + '%');
  270. });
  271. // 文件上传成功,给item添加成功class, 用样式标记上传成功。
  272. bindObj.on('uploadSuccess', function (file, response) {
  273. succeedMessage("上传成功");
  274. //隐藏域ID,用于往后台传值
  275. $("#" + file.id + "DocUrl").val(response);
  276. $('#' + file.id).find('p.state').text('已上传');
  277. });
  278. // 完成上传完了,成功或者失败,先删除进度条。
  279. bindObj.on('uploadComplete', function (file) {
  280. // $( '#'+file.id ).find('.progress').fadeOut();
  281. $('#' + file.id).find('.progress').remove();
  282. });
  283. // 文件上传失败,显示上传出错。
  284. bindObj.on('uploadError', function (file, response) {
  285. //errorMessage(response);
  286. errorMessage('上传出错');
  287. $('#' + file.id).find('p.state').text('上传出错');
  288. //移除上传失败的隐藏域input值
  289. $('#' + file.id + 'DocUrl').remove();
  290. //不显示错误文件
  291. $('#' + file.id).remove();
  292. });
  293. // 其他错误
  294. bindObj.on('error', function (type) {
  295. if ("Q_EXCEED_SIZE_LIMIT" == type) {
  296. errorMessage("文件大小超出了限制");
  297. } else if ("Q_TYPE_DENIED" == type) {
  298. errorMessage("文件类型不满足");
  299. } else if ("Q_EXCEED_NUM_LIMIT" == type) {
  300. errorMessage("上传数量超过限制");
  301. } else if ("F_DUPLICATE" == type) {
  302. errorMessage("文件选择重复");
  303. } else if ("F_EXCEED_SIZE" == type) {
  304. errorMessage("文件大小超出了限制");
  305. } else {
  306. errorMessage("上传过程中出错");
  307. }
  308. });
  309. },
  310. /**
  311. * 设置图片上传的进度条的id
  312. */
  313. setUploadBarId: function (id) {
  314. this.uploadBarId = id;
  315. }
  316. };
  317. /**
  318. * 文件
  319. * @param options
  320. */
  321. var $WebUploadDocList = function (options) {
  322. this.auto = options.auto || true; //开启自动上传
  323. this.uploadBtnId = options.uploadBtnId || '#filePicker'; //选择上传按钮,id|class自己定义
  324. this.uploadPreId = options.docPreId || 'fileListPre'; //预览ID
  325. this.uploadUrl = options.serverUrl || pagePath + '/upload/docs'; //上传URL
  326. this.swf = pagePath + '/h-ui/lib/webuploader/0.1.5/Uploader.swf';
  327. this.docUrl = options.hiddenDocUrl || 'fileDown.file_id'; //隐藏域name,用于往后台传值
  328. this.fileSizeLimit = 1024 * 1024 * 1024 * 5; //验证文件总大小是否超出限制, 超出则不允许加入队列
  329. this.fileSingleSizeLimit = 1024 * 1024 * 1024 * 5; // 验证单个文件大小是否超出限制, 超出则不允许加入队列
  330. //this.fileSizeLimit = options.fileSizeLimit || 100 * 1024 * 1024; //验证文件总大小是否超出限制, 超出则不允许加入队列
  331. //this.fileSingleSizeLimit = options.fileSingleSizeLimit || 2 * 1024 * 1024; // 验证单个文件大小是否超出限制, 超出则不允许加入队列
  332. this.fileNumLimit = options.fileNumLimit || undefined; // 验证文件总数量, 超出则不允许加入队列
  333. this.accept = options.accept; //指定接受哪些类型的文件
  334. this.append = options.append || false; //是否开启追加多个
  335. this.uploadBarId = null; //上传的进度条的id
  336. };
  337. var file_index = 0;
  338. $WebUploadDocList.prototype = {
  339. /**
  340. * 初始化webUploader
  341. */
  342. init: function () {
  343. IEVersion();
  344. var uploader = this.create();
  345. this.bindEvent(uploader);
  346. return uploader;
  347. },
  348. /**
  349. * 创建webuploader对象
  350. */
  351. create: function () {
  352. return WebUploader.create({
  353. auto: this.auto,
  354. pick: {
  355. id: this.uploadBtnId,
  356. multiple: false // 只上传一个
  357. },
  358. accept: this.accept,
  359. swf: this.swf,
  360. disableGlobalDnd: true,
  361. duplicate: true,
  362. server: this.uploadUrl,
  363. fileSizeLimit: this.fileSizeLimit,
  364. fileSingleSizeLimit: this.fileSingleSizeLimit,
  365. fileNumLimit: this.fileNumLimit
  366. });
  367. },
  368. /**
  369. * 绑定事件
  370. */
  371. bindEvent: function (bindObj) {
  372. var me = this;
  373. //当文件被加入队列以后触发
  374. bindObj.on('fileQueued', function (file) {
  375. /*var $li = $(
  376. '<div id="' + file.id + '" class="item">' +
  377. '<h4 class="info">' + file.name + '</h4>' +
  378. '<input type="hidden" name="' + me.docUrl + '" id="' + file.id + 'DocUrl" />' +
  379. '<a href="javascript:;" onclick="delDoc(this);">删除</a>' +
  380. '<p class="state">等待上传...</p>' +
  381. '</div>'
  382. );*/
  383. //'<a href="${fileUrl} + ${czMaterials.file_url}" target="_blank" text="${czMaterials.file_name}" style="color: #5b98dd;"></a>'
  384. var str = ''
  385. str += '<tr class="text-c">'
  386. str += '<td><input name="fileDownList[' + file_index + '].file_name" maxlength="40"/><input hidden id="' + file.id + '" name="fileDownList[' + file_index + '].file_id" /></td>';
  387. str += '<td>' + file.name + '</td>';
  388. str += '<td><a href="javascript:;" onclick="delDocList(this);">删除</a></td>';
  389. str += '</tr>'
  390. /*if (me.append) {
  391. $("#uploadList").append(str);
  392. } else {
  393. $("#uploadList").html(str);
  394. }*/
  395. $("#uploadList").append(str);
  396. file_index++;
  397. });
  398. // 文件上传过程中创建进度条实时显示。
  399. bindObj.on('uploadProgress', function (file, percentage) {
  400. var $div = $('#progress'),
  401. $percent = $div.find('.progress .progress-bar');
  402. // 避免重复创建
  403. if (!$percent.length) {
  404. $percent = $('<div class="progress progress-striped active">' +
  405. '<div class="progress-bar" role="progressbar" style="width: 0%">' +
  406. '</div>' +
  407. '</div>').appendTo($div).find('.progress-bar');
  408. }
  409. $('#state').text('上传中');
  410. $percent.css('width', percentage * 100 + '%');
  411. if (percentage == 1) {
  412. $('#state').text('上传完成');
  413. }
  414. });
  415. // 文件上传成功,给item添加成功class, 用样式标记上传成功。
  416. bindObj.on('uploadSuccess', function (file, response) {
  417. succeedMessage("上传成功");
  418. //隐藏域ID,用于往后台传值
  419. //$("#" + file.id + "DocUrl").val(response);
  420. //$( '#'+file.id ).find('p.state').text('已上传');
  421. //console.log(file)
  422. //console.log(response)
  423. $("#" + file.id ).val(response);
  424. });
  425. // 完成上传完了,成功或者失败,先删除进度条。
  426. bindObj.on('uploadComplete', function (file) {
  427. // $( '#'+file.id ).find('.progress').fadeOut();
  428. $('#progress').find('.progress').remove();
  429. //$('.state').remove();
  430. });
  431. // 文件上传失败,显示上传出错。
  432. bindObj.on('uploadError', function (file, response) {
  433. //errorMessage(response);
  434. errorMessage('上传出错');
  435. $('#' + file.id).find('p.state').text('上传出错');
  436. //移除上传失败的隐藏域input值
  437. $('#' + file.id + 'DocUrl').remove();
  438. //不显示错误文件
  439. $('#' + file.id).remove();
  440. });
  441. // 其他错误
  442. bindObj.on('error', function (type) {
  443. if ("Q_EXCEED_SIZE_LIMIT" == type) {
  444. errorMessage("文件大小超出了限制");
  445. } else if ("Q_TYPE_DENIED" == type) {
  446. errorMessage("文件类型不满足");
  447. } else if ("Q_EXCEED_NUM_LIMIT" == type) {
  448. errorMessage("上传数量超过限制");
  449. } else if ("F_DUPLICATE" == type) {
  450. errorMessage("文件选择重复");
  451. } else if ("F_EXCEED_SIZE" == type) {
  452. errorMessage("文件大小超出了限制");
  453. } else {
  454. errorMessage("上传过程中出错");
  455. }
  456. });
  457. },
  458. /**
  459. * 设置图片上传的进度条的id
  460. */
  461. setUploadBarId: function (id) {
  462. this.uploadBarId = id;
  463. }
  464. };
  465. window.$WebUploadDocList = $WebUploadDocList;
  466. window.$WebUploadPicture = $WebUploadPicture;
  467. window.$WebUploadDoc = $WebUploadDoc;
  468. }());
  469. function delDoc(obj) {
  470. var $this = $(obj);
  471. $this.parent("div").remove();
  472. }
  473. function delDocList(obj) {
  474. var $this = $(obj);
  475. $this.parent().parent().remove();
  476. }