Extjs 表单 显示图片 + 上传

弄了一天的图片上传(也就是在表单里面显示图片,预览图片),显示,通过网上找资料终于弄好了。现在整理一下,贴到这,下次要再用到也方便查询了。。。

Java代码 收藏代码
  1. //uploadFile.js
  2. Ext.onReady(function(){
  3. varfileForm=newExt.form.FormPanel({
  4. title:"",
  5. renderTo:"fileUpload",
  6. fileUpload:true,
  7. layout:"form",
  8. id:"fileUploadForm",
  9. items:[{
  10. id:'upload',
  11. name:'upload',
  12. inputType:"file",
  13. fieldLabel:'上传图片',
  14. xtype:'textfield',
  15. anchor:'40%'
  16. },{
  17. xtype:'box',
  18. id:'browseImage',
  19. fieldLabel:"预览图片",
  20. autoEl:{
  21. width:300,
  22. height:350,
  23. tag:'img',
  24. //type:'image',
  25. src:Ext.BLANK_IMAGE_URL,
  26. style:'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);',
  27. complete:'off',
  28. id:'imageBrowse'
  29. }
  30. }],
  31. //form事件
  32. listeners:{
  33. 'render':function(f){
  34. //
  35. this.form.findField('upload').on('render',function(){
  36. //通過change事件,图片也动态跟踪选择的图片变化
  37. Ext.get('upload').on('change',
  38. function(field,newValue,oldValue){
  39. //得到选择的图片路径
  40. varurl='file://'
  41. +Ext.get('upload').dom.value;
  42. //alert("url="+url);
  43. //是否是规定的图片类型
  44. if(img_reg.test(url)){
  45. if(Ext.isIE){
  46. varimage=Ext.get('imageBrowse').dom;
  47. image.src=Ext.BLANK_IMAGE_URL;//覆盖原来的图片
  48. image.filters
  49. .item("DXImageTransform.Microsoft.AlphaImageLoader").src=url;
  50. }//支持FF
  51. else{
  52. Ext.get('imageBrowse').dom.src=Ext
  53. .get('upload').dom.files
  54. .item(0).getAsDataURL()
  55. }
  56. }
  57. },this);
  58. },this);
  59. }
  60. },
  61. buttons:[{
  62. text:"提交",
  63. name:"submit",
  64. handler:submit
  65. }]
  66. });
  67. //上传图片类型
  68. varimg_reg=/\.([jJ][pP][gG]){1}$|\.([jJ][pP][eE][gG]){1}$|\.([gG][iI][fF]){1}$|\.([pP][nN][gG]){1}$|\.([bB][mM][pP]){1}$/
  69. });
  70. //上傳圖片到服务器,
  71. functionsubmit(){
  72. Ext.getCmp("fileUploadForm").getForm().submit({
  73. url:"uploadAction.action",
  74. method:"POST",
  75. success:function(form,action){
  76. alert("success");
  77. },
  78. failure:function(form,action){
  79. alert("failure");
  80. }
  81. });
  82. }


传入后台处理,使用struts2
Java代码 收藏代码
  1. packagecom.cocobra.action;
  2. importjava.io.*;
  3. importjava.util.Date;
  4. importjava.util.UUID;
  5. importorg.apache.struts2.ServletActionContext;
  6. importcom.opensymphony.xwork2.ActionSupport;
  7. publicclassUploadActionextendsActionSupport{
  8. /**
  9. *
  10. */
  11. privatestaticfinallongserialVersionUID=1L;
  12. privateFileupload;
  13. privateStringuploadContentType;
  14. privateStringuploadFileName;//fileName前面必須和uplaod一致,不然找不到文件
  15. publicFilegetUpload(){
  16. returnupload;
  17. }
  18. publicvoidsetUpload(Fileupload){
  19. this.upload=upload;
  20. }
  21. publicStringgetUploadContentType(){
  22. returnuploadContentType;
  23. }
  24. publicvoidsetUploadContentType(StringuploadContentType){
  25. this.uploadContentType=uploadContentType;
  26. }
  27. publicStringgetUploadFileName(){
  28. returnuploadFileName;
  29. }
  30. publicvoidsetUploadFileName(StringuploadFileName){
  31. this.uploadFileName=uploadFileName;
  32. }
  33. //上传文件的文件名
  34. privateStringgetFileExp(Stringname){
  35. intpos=name.lastIndexOf(".");
  36. returnname.substring(pos);
  37. }
  38. privatestaticfinalintBUFFER_SIZE=16*1024;
  39. publicStringexecute()throwsException{
  40. Dated=newDate();
  41. System.out.println("uploadFileName="+this.uploadFileName);
  42. //upload--wapps下面的文件夹,用来存放图片
  43. StringtoSrc=ServletActionContext.getServletContext().getRealPath("upload")+"/"+d.getTime()+getFileExp(this.uploadFileName);//使用時間戳作為文件名
  44. System.out.println("toFile="+toSrc);
  45. FiletoFile=newFile(toSrc);
  46. writeFile(this.upload,toFile);
  47. returnSUCCESS;
  48. }
  49. privatestaticvoidwriteFile(Filesrc,Filedst){
  50. System.out.println("==文件寫入==");
  51. try{
  52. InputStreamin=null;
  53. OutputStreamout=null;
  54. try{
  55. in=newBufferedInputStream(newFileInputStream(src),
  56. BUFFER_SIZE);
  57. out=newBufferedOutputStream(newFileOutputStream(dst),
  58. BUFFER_SIZE);
  59. byte[]buffer=newbyte[BUFFER_SIZE];
  60. while(in.read(buffer)>0){
  61. out.write(buffer);
  62. }
  63. }finally{
  64. if(null!=in){
  65. in.close();
  66. }
  67. if(null!=out){
  68. out.close();
  69. }
  70. }
  71. }catch(Exceptione){
  72. e.printStackTrace();
  73. }
  74. System.out.println("写入成功!");
  75. }
  76. }


struts2中的struts.xml 配置
Java代码 收藏代码
  1. <actionname="uploadAction"class="com.cocobra.action.UploadAction">
  2. <interceptor-refname="fileUpload">
  3. <!--拦截器strut2自带的,指定上传文件的格式,如果不符合规定,将会自动拦截下来-->
  4. <paramname="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param>
  5. <paramname="maximumSize">20000000000</param>
  6. </interceptor-ref>
  7. <interceptor-refname="defaultStack"/>
  8. <resultname="success">/index.jsp</result>
  9. </action>
2
0

你可能感兴趣的:(ExtJs)