ExtJS 3.4.0 - Upload File from Ext.Window

最近使用到ExtJS Window介面來結合上傳檔案的功能,雖然官方有提供範例,不過還是改寫

了前端的code及兜了一個server-side為JSP處理的部分(原本為PHP)來做個紀錄。

主要是透過介面上的按鈕來叫出Ext.Window的視窗,搭配Ext.FormPanel來送出表單,再透過

JSP程式內使用SmartUpload jar撰寫Java code來將檔案寫入至相關目錄,並回傳訊息至前端。


前端code

需先載入

ux/fileuploadfield/FileUploadField.js
ext-all.js
adapter/ext/ext-base.js
resources/css/ext-all.css
ux/fileuploadfield/css/fileuploadfield.css

相關路徑可自行參酌ExtJS example
  1. Ext.onReady(function(){
  2. Ext.QuickTips.init();
  3. var dlgForm = new Ext.FormPanel({
  4. //use your own!
  5. url: 'file-upload.jsp',
  6. labelAlign: 'top',
  7. frame: true,
  8. border: false,
  9. fileUpload: true,
  10. autoHeight: true,
  11. defaults: {
  12. anchor: '95%',
  13. allowBlank: false,
  14. msgTarget: 'side'
  15. },
  16. items: [
  17. {
  18. xtype: 'fileuploadfield',
  19. id: 'form-file',
  20. fieldLabel: 'File with data',
  21. name: 'dataFile',
  22. buttonText: '',
  23. width: 100,
  24. buttonCfg: {
  25. iconCls: 'icon-file-upload'
  26. }
  27. }]
  28. });
  29. var dlgWindow = new Ext.Window({
  30. id: 'data-upload',
  31. layout: 'fit',
  32. width: 350,
  33. autoHeight: true,
  34. plain: true,
  35. modal: true,
  36. border: false,
  37. title: 'Upload data file',
  38. items: dlgForm,
  39. buttons: [ {
  40. text: 'Submit',
  41. scope: this,
  42. handler: function() {
  43. dlgForm.getForm().submit({
  44. waitMsg: 'Saving file...',
  45. success: function (form, action) {
  46. var json_t = Ext.util.JSON.decode(action.response.responseText);
  47. Ext.Msg.alert('Success', json_t.info);
  48. dlgForm.getForm().reset();
  49. dlgWindow.hide();
  50. },
  51. failure: function(form, action) {
  52. var json_t = Ext.util.JSON.decode(action.response.responseText);
  53. Ext.Msg.alert('Failed', json_t.info);
  54. }
  55. });
  56. }
  57. }, {
  58. text: 'Cancel',
  59. handler: function() {
  60. dlgWindow.hide();
  61. }
  62. } ]
  63. });
  64. Ext.get("uploadxls").on('click', function(){
  65. dlgWindow.show();
  66. });
  67. }

針對dlgWindow變數定義相關的Window屬性設定,並搭配button's id => uploadxls來顯示該

視窗。dlgForm定義了上傳檔案的項目,並且設定post的url檔案。


後端code => file-upload.jsp

使用到套件為
java-json.jar
smartupload.jar

  1. try{
  2. response.setHeader("Cache-Control", "no-cache");
  3. SmartUpload su = new SmartUpload();
  4. su.initialize(pageContext);
  5. su.setAllowedFilesList("xls");
  6. su.upload();
  7. com.jspsmart.upload.File file = su.getFiles().getFile(0);
  8. System.out.println("file name:" + file.getFileName());
  9. file.saveAs("uploadFileTest.xls", su.SAVE_PHYSICAL);
  10. JSONObject json = new JSONObject();
  11. json.put("success", true);
  12. json.put("info", "upload success!");
  13. response.getWriter().print(json.toString());
  14. response.getWriter().flush();
  15. }catch(Exception e){
  16. e.printStackTrace();
  17. response.getWriter().print(e.getMessage());
  18. }finally {
  19. try {
  20. response.getWriter().close();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }

在這邊要注意的是,response print的內容為JSON格式,且若指定的success = true,在前端

submit後才會進入success function,否則會進入failure function。

console.log印出action的訊息


若回傳的訊息沒設定success = true,則result屬性的值為false,此時即使後端處理得很順利

還是會進入failure function內處理。

PS. 若後來才追加JSON格式success = true的部分,記得在code前面的header設定no-cache,否

則會被cache住。

官方文件

 : Object
The decoded response object containing a boolean success property and other, action-specific properties.
Available since: 1.1.0

DEMO



由於沒特別指定路徑,因此檔案就跑到根目錄的位置

留言