MFileField.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. submitValue: true,
  10. columnWidth: 1,
  11. cls: 'multi-file-field',
  12. minHeight: 22,
  13. collapsible: true,
  14. layout:'column',
  15. files: [],
  16. initComponent: function() {
  17. var me = this;
  18. this.title = this.fieldLabel;
  19. this.files.length = 0;
  20. Ext.apply(me, {
  21. items: [{
  22. xtype: 'hidden',
  23. name: 'fileIds'
  24. }, {
  25. xtype: 'filefield',
  26. name: 'file',
  27. buttonText: '浏览文件(≤20MB)',
  28. buttonOnly: true,
  29. hideLabel: true,
  30. columnWidth: 1,
  31. float: 'left',
  32. buttonConfig:{
  33. cls:'x-filefield-button',
  34. style: {
  35. float: 'left',
  36. margin: '0 0 5px 0'
  37. }
  38. },
  39. createFileInput : function() {
  40. this.fileInputEl = this.button.el.createChild({
  41. name: this.getName(),
  42. cls: Ext.baseCSSPrefix + 'form-file-input',
  43. tag: 'input',
  44. type: 'file',
  45. multiple:'multiple',
  46. size: 1
  47. }).on('change', this.onFileChange, this);
  48. },
  49. listeners: {
  50. change: function(field){
  51. if(field.value){
  52. me.upload(field);
  53. }
  54. }
  55. }
  56. }, {
  57. xtype: 'form',
  58. layout: 'column',
  59. cls: 'form-file-tags',
  60. columnWidth: 1,
  61. bodyPadding:'0 0 5px 5px',
  62. items: [],
  63. }],
  64. listeners: {
  65. change: function() {
  66. console.log('field value change', arguments);
  67. }
  68. }
  69. });
  70. this.callParent(arguments);
  71. if(this.value || this.defalutValue) {
  72. me.initSetValue(this.value || this.defalutValue);
  73. }
  74. },
  75. initSetValue: function(v) {
  76. this.setValue(v, true);
  77. },
  78. setValue: function(v, init){
  79. var me = this,
  80. oldValue = me.getValue(),
  81. filesInfo,
  82. newValue;
  83. me.clearAll();
  84. me.getFilesInfo(v).then(function(res) {
  85. filesInfo = res.data;
  86. newValue = me.addFiles(filesInfo);
  87. me.items.items[0].setValue(newValue);
  88. if(!init) {
  89. me.publishState('value', newValue);
  90. me.fireEvent('change', this, newValue, oldValue);
  91. }
  92. });
  93. },
  94. getValue: function (){
  95. return this.items.items[0].value;
  96. },
  97. getValueField: function () {
  98. return this.items.items[0];
  99. },
  100. getFileForm: function() {
  101. return this.items.items[2];
  102. },
  103. isValid: function () {
  104. return this.getValueField().isValid();
  105. },
  106. isDirty: function () {
  107. return this.getValueField().isDirty();
  108. },
  109. setReadOnly: function (val) {
  110. var me = this,
  111. items = me.items.items,
  112. fileIdsField = items[0],
  113. fileField = items[1];
  114. fileIdsField.setReadOnly(val);
  115. fileField.button.setDisabled(val);
  116. },
  117. resetOriginalValue: function() {
  118. this.getValueField().resetOriginalValue();
  119. },
  120. addFiles: function(fileInfos) {
  121. var me = this,
  122. items = me.items.items,
  123. value = me.getValue(),
  124. oldValue = value,
  125. newValue = value;
  126. Ext.Array.forEach(fileInfos, function(fileInfo) {
  127. if(!me.contains(fileInfo)) {
  128. newValue = (newValue || '') + fileInfo.id + ';';
  129. me.files.push(fileInfo);
  130. me.insertFileTag(fileInfo);
  131. }
  132. });
  133. return newValue;
  134. },
  135. addFile: function(fileInfo) {
  136. var me = this,
  137. items = me.items.items,
  138. value = me.getValue(),
  139. oldValue = value,
  140. newValue = value;
  141. if(!me.contains(fileInfo)) {
  142. newValue = (newValue || '') + fileInfo.id + ';';
  143. items[0].setValue(newValue);
  144. this.files.push(fileInfo);
  145. this.insertFileTag(fileInfo);
  146. this.publishState('value', newValue);
  147. this.fireEvent('change', this, newValue, oldValue);
  148. }
  149. },
  150. insertFileTag: function(fileInfo) {
  151. var me = this,
  152. fileForm = me.getFileForm(),
  153. id = fileInfo.id,
  154. name = fileInfo.name,
  155. path = fileInfo.path,
  156. size = fileInfo.size,
  157. fileText, fieldWidth, newFileTag;
  158. if(!id) {
  159. return;
  160. }
  161. fileText = name + " (" + Ext.util.Format.fileSize(size) + ")";
  162. fieldWidth = Math.min((me.getStrLength(fileText) + 25) / 200, .8);
  163. newFileTag = Ext.create('Ext.ux.form.field.FileTag', {
  164. fileName: name,
  165. value: name,
  166. columnWidth: fieldWidth,
  167. readOnly: false,
  168. editable: false,
  169. filePath: path,
  170. fileSize: size,
  171. fileId: id,
  172. fieldStyle: 'background:#E0EEEE;'
  173. });
  174. fileForm.add(newFileTag);
  175. },
  176. /**
  177. * 上传附件
  178. */
  179. upload: function(fileField){
  180. var me = this;
  181. var fileEl = fileField.fileInputEl.dom;
  182. var fd = new FormData();
  183. fd.append('file', fileEl.files[0]);
  184. fd.append('folderId', 0);
  185. me.setLoading(true);
  186. Ext.Ajax.request({
  187. url: '/api/file/upload',//这里是填写需要跨域访问的URL
  188. cors: true,
  189. useDefaultXhrHeader: false,
  190. method: 'post',
  191. rawData: fd,
  192. headers: {
  193. // 'Access-Control-Allow-Origin': '*',
  194. // 'Authorization': uas.util.State.get('session').token,
  195. //"Content-Type": 'multipart/form-data' //文件上传的格式,
  196. "Content-Type":null
  197. },
  198. success: function (response, opts) {
  199. me.setLoading(false);
  200. var res = Ext.decode(response.responseText);
  201. if(res.success){
  202. var data = res.data;
  203. me.addFile(data);
  204. }else{
  205. console.error('failure', res);
  206. }
  207. },
  208. failure: function (response, opts) {
  209. me.setLoading(false);
  210. console.error('failure', response);
  211. }
  212. });
  213. },
  214. getFilesInfo: function(value){
  215. var me = this;
  216. me.setLoading(true);
  217. return new Ext.Promise(function (resolve, reject) {
  218. Ext.Ajax.request({
  219. url : '/api/file/info',
  220. params: {
  221. ids: value
  222. },
  223. method : 'GET',
  224. success: function (response, opts) {
  225. me.setLoading(false);
  226. var res = Ext.decode(response.responseText);
  227. if(res.success){
  228. resolve(res);
  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. var oldValue = this.getValue();
  250. this.getFileForm().removeAll();
  251. this.items.items[0].value = '';
  252. this.files.length = 0;
  253. this.publishState('value', '');
  254. this.fireEvent('change', this, '', oldValue);
  255. },
  256. remove: function(fileInfo) {
  257. var value = this.getValue(),
  258. fileTags,
  259. foundFileTag,
  260. idx,
  261. newValue;
  262. if(typeof fileInfo === 'string') {
  263. fileInfo = { id: fileInfo };
  264. }
  265. fileTags = this.getFileForm().items.items;
  266. foundFileTag = fileTags.find(function(ft) {
  267. return ft.fileId === fileInfo.id;
  268. });
  269. idx = this.files.findIndex(function(f) {
  270. return f.id === fileInfo.id;
  271. });
  272. if(foundFileTag && idx > -1) {
  273. newValue = value.replace(fileInfo.id + ';', '');
  274. this.getFileForm().remove(foundFileTag);
  275. this.items.items[0].setValue(newValue);
  276. this.files.splice(idx, 1);
  277. this.publishState('value', newValue);
  278. this.fireEvent('change', this, newValue, value);
  279. }
  280. },
  281. getStrLength: function(str) {
  282. for (var len = str.length, c = 0, i = 0; i < len; i++)
  283. str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126 ? c += 2 : c++;
  284. return c;
  285. },
  286. getModelData: function(includeEmptyText, isSubmitting) {
  287. var me = this,
  288. data = null;
  289. // Note that we need to check if this operation is being called from a Submit action because displayfields aren't
  290. // to be submitted, but they can call this to get their model data.
  291. if (!me.disabled && (me.submitValue || !isSubmitting)) {
  292. data = {};
  293. data[me.getFieldIdentifier()] = me.getValue();
  294. }
  295. return data;
  296. },
  297. getSubmitData: function() {
  298. var me = this,
  299. data = null;
  300. if (!me.disabled && me.submitValue) {
  301. data = {};
  302. data[me.getName()] = '' + me.getValue();
  303. }
  304. return data;
  305. },
  306. getFieldIdentifier: function () {
  307. return this.isEditorComponent ? this.dataIndex : this.name;
  308. },
  309. getName: function() {
  310. return this.name;
  311. },
  312. // checkUploadAmount:function(form){
  313. // var files = form.getEl().down('input[type=file]').dom.files;
  314. // var amounts = 0;
  315. // for (var i = 0; i < files.length; i++) {
  316. // amounts = amounts + files[i].size
  317. // }
  318. // if (amounts>104857600) {
  319. // Ext.MessageBox.alert("警告","对不起,上传文件总大小超过100m");
  320. // return false
  321. // }
  322. // return true;
  323. // },
  324. // checkFile:function(fileName){
  325. // var arr=['php','php2','php3', 'php5', 'phtml', 'asp', 'aspx', 'ascx', 'jsp', 'cfm', 'cfc', 'pl','pl','bat', 'dll', 'reg', 'cgi','war'];
  326. // var suffix=fileName.substring(fileName.lastIndexOf(".")+1);
  327. // return Ext.Array.contains(arr,suffix);
  328. // },
  329. });