MFileField.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**
  2. * 多文件上传下载
  3. */
  4. Ext.define('Ext.ux.form.field.MFileField', {
  5. extend: 'Ext.form.FieldSet',
  6. alias: 'widget.mfilefield',
  7. defaultBindProperty: 'value',
  8. isFormField:true,
  9. columnWidth: 1,
  10. cls: 'multi-file-field',
  11. minHeight: 22,
  12. collapsible: true,
  13. layout:'column',
  14. files: [],
  15. initComponent: function() {
  16. var me = this;
  17. this.title = this.fieldLabel;
  18. if(this.value || this.defalutValue) {
  19. me.getFilesInfo(this.value || this.defalutValue).then(function(res) {
  20. var filesInfo = res.data;
  21. me.addFiles(filesInfo);
  22. });
  23. }
  24. Ext.apply(me, {
  25. items: [{
  26. xtype: 'hidden',
  27. name: 'fileIds'
  28. }, {
  29. xtype: 'filefield',
  30. name: 'file',
  31. buttonText: '浏览文件(≤20MB)',
  32. buttonOnly: true,
  33. hideLabel: true,
  34. columnWidth: 1,
  35. float: 'left',
  36. buttonConfig:{
  37. cls:'x-filefield-button',
  38. style: {
  39. float: 'left',
  40. margin: '0 0 5px 0'
  41. }
  42. },
  43. createFileInput : function() {
  44. this.fileInputEl = this.button.el.createChild({
  45. name: this.getName(),
  46. cls: Ext.baseCSSPrefix + 'form-file-input',
  47. tag: 'input',
  48. type: 'file',
  49. multiple:'multiple',
  50. size: 1
  51. }).on('change', this.onFileChange, this);
  52. },
  53. listeners: {
  54. change: function(field){
  55. if(field.value){
  56. me.upload(field);
  57. }
  58. }
  59. }
  60. }, {
  61. xtype: 'form',
  62. layout: 'column',
  63. cls: 'form-file-tags',
  64. columnWidth: 1,
  65. bodyPadding:'0 0 5px 5px',
  66. items: [],
  67. }],
  68. listeners: {
  69. change: function() {
  70. console.log('field value change', arguments);
  71. }
  72. }
  73. });
  74. this.callParent(arguments);
  75. },
  76. setValue: function(v){
  77. var me = this,
  78. items = me.items.items,
  79. files = me.files,
  80. newValue = '',
  81. filesInfo;
  82. me.getFilesInfo(v).then(function(res) {
  83. var filesInfo = res.data;
  84. me.addFiles(filesInfo);
  85. });
  86. },
  87. getValue: function (){
  88. return this.items.items[0].value;
  89. },
  90. getValueField: function () {
  91. return this.items.items[0];
  92. },
  93. getFileForm: function() {
  94. return this.items.items[2];
  95. },
  96. isValid: function () {
  97. return this.getValueField().isValid();
  98. },
  99. isDirty: function () {
  100. return this.getValueField().isDirty();
  101. },
  102. setReadOnly: function (val) {
  103. var me = this,
  104. items = me.items.items,
  105. fileIdsField = items[0],
  106. fileField = items[1];
  107. fileIdsField.setReadOnly(val);
  108. fileField.button.setDisabled(val);
  109. },
  110. resetOriginalValue: function() {
  111. this.getValueField().resetOriginalValue();
  112. },
  113. addFiles: function(fileInfos) {
  114. var me = this,
  115. items = me.items.items,
  116. value = me.getValue(),
  117. id,
  118. oldValue = value,
  119. newValue = value;
  120. Ext.Array.forEach(fileInfos, function(fileInfo) {
  121. if(!me.contains(fileInfo)) {
  122. newValue = (newValue || '') + fileInfo.id + ';';
  123. me.files.push(fileInfo);
  124. me.insertFileTag(fileInfo);
  125. }
  126. });
  127. items[0].setValue(newValue);
  128. this.publishState('value', newValue);
  129. this.fireEvent('change', this, newValue, oldValue);
  130. },
  131. addFile: function(fileInfo) {
  132. var me = this,
  133. items = me.items.items,
  134. value = me.getValue(),
  135. oldValue = value,
  136. newValue = value;
  137. if(!me.contains(fileInfo)) {
  138. newValue = (newValue || '') + fileInfo.id + ';';
  139. items[0].setValue(newValue);
  140. this.files.push(fileInfo);
  141. this.insertFileTag(fileInfo);
  142. this.publishState('value', newValue);
  143. this.fireEvent('change', this, newValue, oldValue);
  144. }
  145. },
  146. insertFileTag: function(fileInfo) {
  147. var me = this,
  148. fileForm = me.getFileForm(),
  149. id = fileInfo.id,
  150. name = fileInfo.name,
  151. path = fileInfo.fullName,
  152. size = fileInfo.size,
  153. fileText, fieldWidth, newFileTag;
  154. if(!id) {
  155. return;
  156. }
  157. fileText = name + " (" + Ext.util.Format.fileSize(size) + ")";
  158. fieldWidth = Math.min((me.getStrLength(fileText) + 25) / 200, .8);
  159. newFileTag = Ext.create('Ext.ux.form.field.FileTag', {
  160. fileName: name,
  161. value: name,
  162. columnWidth: fieldWidth,
  163. readOnly: false,
  164. editable: false,
  165. filepath: path,
  166. filesize: size,
  167. _id: id,
  168. fieldStyle: 'background:#E0EEEE;'
  169. });
  170. fileForm.add(newFileTag);
  171. },
  172. /**
  173. * 上传附件
  174. */
  175. upload: function(fileField){
  176. var me = this;
  177. var fileEl = fileField.fileInputEl.dom;
  178. var fd = new FormData();
  179. fd.append('file', fileEl.files[0]);
  180. fd.append('folderId', 0);
  181. me.setLoading(true);
  182. Ext.Ajax.request({
  183. url: '/api/file/upload',//这里是填写需要跨域访问的URL
  184. cors: true,
  185. useDefaultXhrHeader: false,
  186. method: 'post',
  187. rawData: fd,
  188. headers: {
  189. // 'Access-Control-Allow-Origin': '*',
  190. // 'Authorization': uas.util.State.get('session').token,
  191. //"Content-Type": 'multipart/form-data' //文件上传的格式,
  192. "Content-Type":null
  193. },
  194. success: function (response, opts) {
  195. me.setLoading(false);
  196. var res = Ext.decode(response.responseText);
  197. if(res.success){
  198. console.log('success');
  199. var data = res.data;
  200. me.addFile(data);
  201. }else{
  202. console.error('failure');
  203. }
  204. },
  205. failure: function (response, opts) {
  206. me.setLoading(false);
  207. console.error('failure');
  208. }
  209. });
  210. },
  211. getFilesInfo: function(value){
  212. var me = this;
  213. me.setLoading(true);
  214. return new Ext.Promise(function (resolve, reject) {
  215. Ext.Ajax.request({
  216. url : '/api/file/info',
  217. params: {
  218. ids: value
  219. },
  220. method : 'GET',
  221. success: function (response, opts) {
  222. me.setLoading(false);
  223. var res = Ext.decode(response.responseText);
  224. if(res.success){
  225. resolve(res);
  226. // if(me.contains()) {
  227. // }
  228. // me.addFiles(res.data);
  229. }else{
  230. reject(res);
  231. }
  232. },
  233. failure: function (response, opts) {
  234. me.setLoading(false);
  235. reject(response);
  236. }
  237. });
  238. })
  239. },
  240. contains: function(fileInfo) {
  241. if(typeof fileInfo === 'string') { // 可能直接传id
  242. fileInfo = { id: fileInfo };
  243. }
  244. return !!Ext.Array.findBy(this.files, function(f) {
  245. return f.id === fileInfo.id;
  246. });
  247. },
  248. clearAll: function() {
  249. this.getFileForm().removeAll();
  250. this.setValue('');
  251. this.files.length = 0;
  252. },
  253. getStrLength: function(str) {
  254. for (var len = str.length, c = 0, i = 0; i < len; i++)
  255. str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126 ? c += 2 : c++;
  256. return c;
  257. },
  258. // checkUploadAmount:function(form){
  259. // var files = form.getEl().down('input[type=file]').dom.files;
  260. // var amounts = 0;
  261. // for (var i = 0; i < files.length; i++) {
  262. // amounts = amounts + files[i].size
  263. // }
  264. // if (amounts>104857600) {
  265. // Ext.MessageBox.alert("警告","对不起,上传文件总大小超过100m");
  266. // return false
  267. // }
  268. // return true;
  269. // },
  270. // checkFile:function(fileName){
  271. // var arr=['php','php2','php3', 'php5', 'phtml', 'asp', 'aspx', 'ascx', 'jsp', 'cfm', 'cfc', 'pl','pl','bat', 'dll', 'reg', 'cgi','war'];
  272. // var suffix=fileName.substring(fileName.lastIndexOf(".")+1);
  273. // return Ext.Array.contains(arr,suffix);
  274. // },
  275. });