web-upload-object.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * web-upload 工具类
  3. *
  4. * 约定:
  5. * 上传按钮的id = 图片隐藏域id + 'BtnId'
  6. * 图片预览框的id = 图片隐藏域id + 'PreId'
  7. *
  8. * @author fengshuonan
  9. */
  10. (function() {
  11. var $WebUpload = function(pictureId) {
  12. this.pictureId = pictureId;
  13. this.uploadBtnId = pictureId + "BtnId";
  14. this.uploadPreId = pictureId + "PreId";
  15. this.uploadUrl = Feng.ctxPath + '/mgr/upload';
  16. this.fileSizeLimit = 100 * 1024 * 1024;
  17. this.picWidth = 800;
  18. this.picHeight = 800;
  19. this.uploadBarId = null;
  20. };
  21. $WebUpload.prototype = {
  22. /**
  23. * 初始化webUploader
  24. */
  25. init : function() {
  26. var uploader = this.create();
  27. this.bindEvent(uploader);
  28. return uploader;
  29. },
  30. /**
  31. * 创建webuploader对象
  32. */
  33. create : function() {
  34. var webUploader = WebUploader.create({
  35. auto : true,
  36. pick : {
  37. id : '#' + this.uploadBtnId,
  38. multiple : false,// 只上传一个
  39. },
  40. accept : {
  41. title : 'Images',
  42. extensions : 'gif,jpg,jpeg,bmp,png',
  43. mimeTypes : 'image/gif,image/jpg,image/jpeg,image/bmp,image/png'
  44. },
  45. swf : Feng.ctxPath
  46. + '/static/js/plugins/webuploader/Uploader.swf',
  47. disableGlobalDnd : true,
  48. duplicate : true,
  49. server : this.uploadUrl,
  50. fileSingleSizeLimit : this.fileSizeLimit
  51. });
  52. return webUploader;
  53. },
  54. /**
  55. * 绑定事件
  56. */
  57. bindEvent : function(bindedObj) {
  58. var me = this;
  59. bindedObj.on('fileQueued', function(file) {
  60. var $li = $('<div><img width="100px" height="100px"></div>');
  61. var $img = $li.find('img');
  62. $("#" + me.uploadPreId).html($li);
  63. // 生成缩略图
  64. bindedObj.makeThumb(file, function(error, src) {
  65. if (error) {
  66. $img.replaceWith('<span>不能预览</span>');
  67. return;
  68. }
  69. $img.attr('src', src);
  70. }, me.picWidth, me.picHeight);
  71. });
  72. // 文件上传过程中创建进度条实时显示。
  73. bindedObj.on('uploadProgress', function(file, percentage) {
  74. $("#"+me.uploadBarId).css("width",percentage * 100 + "%");
  75. });
  76. // 文件上传成功,给item添加成功class, 用样式标记上传成功。
  77. bindedObj.on('uploadSuccess', function(file,response) {
  78. Feng.success("上传成功");
  79. $("#" + me.pictureId).val(response);
  80. });
  81. // 文件上传失败,显示上传出错。
  82. bindedObj.on('uploadError', function(file) {
  83. Feng.error("上传失败");
  84. });
  85. // 其他错误
  86. bindedObj.on('error', function(type) {
  87. if ("Q_EXCEED_SIZE_LIMIT" == type) {
  88. Feng.error("文件大小超出了限制");
  89. } else if ("Q_TYPE_DENIED" == type) {
  90. Feng.error("文件类型不满足");
  91. } else if ("Q_EXCEED_NUM_LIMIT" == type) {
  92. Feng.error("上传数量超过限制");
  93. } else if ("F_DUPLICATE" == type) {
  94. Feng.error("图片选择重复");
  95. } else {
  96. Feng.error("上传过程中出错");
  97. }
  98. });
  99. // 完成上传完了,成功或者失败
  100. bindedObj.on('uploadComplete', function(file) {
  101. });
  102. },
  103. /**
  104. * 设置图片上传的进度条的id
  105. */
  106. setUploadBarId: function (id) {
  107. this.uploadBarId = id;
  108. }
  109. };
  110. window.$WebUpload = $WebUpload;
  111. }());