亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Extjs 表單 顯示圖片 + 上傳

系統 2077 0
弄了一天的圖片上傳(也就是在表單里面顯示圖片,預覽圖片),顯示,通過網上找資料終于弄好了。現在整理一下,貼到這,下次要再用到也方便查詢了。。。

Java代碼 收藏代碼
  1. //uploadFile.js
  2. Ext.onReady(function(){
  3. varfileForm= new Ext.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. package com.cocobra.action;
  2. import java.io.*;
  3. import java.util.Date;
  4. import java.util.UUID;
  5. import org.apache.struts2.ServletActionContext;
  6. import com.opensymphony.xwork2.ActionSupport;
  7. public class UploadAction extends ActionSupport{
  8. /**
  9. *
  10. */
  11. private static final long serialVersionUID=1L;
  12. private Fileupload;
  13. private StringuploadContentType;
  14. private StringuploadFileName; //fileName前面必須和uplaod一致,不然找不到文件
  15. public FilegetUpload(){
  16. return upload;
  17. }
  18. public void setUpload(Fileupload){
  19. this .upload=upload;
  20. }
  21. public StringgetUploadContentType(){
  22. return uploadContentType;
  23. }
  24. public void setUploadContentType(StringuploadContentType){
  25. this .uploadContentType=uploadContentType;
  26. }
  27. public StringgetUploadFileName(){
  28. return uploadFileName;
  29. }
  30. public void setUploadFileName(StringuploadFileName){
  31. this .uploadFileName=uploadFileName;
  32. }
  33. //上傳文件的文件名
  34. private StringgetFileExp(Stringname){
  35. int pos=name.lastIndexOf( "." );
  36. return name.substring(pos);
  37. }
  38. private static final int BUFFER_SIZE= 16 * 1024 ;
  39. public Stringexecute() throws Exception{
  40. Dated= new Date();
  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= new File(toSrc);
  46. writeFile( this .upload,toFile);
  47. return SUCCESS;
  48. }
  49. private static void writeFile(Filesrc,Filedst){
  50. System.out.println( "==文件寫入==" );
  51. try {
  52. InputStreamin= null ;
  53. OutputStreamout= null ;
  54. try {
  55. in= new BufferedInputStream( new FileInputStream(src),
  56. BUFFER_SIZE);
  57. out= new BufferedOutputStream( new FileOutputStream(dst),
  58. BUFFER_SIZE);
  59. byte []buffer= new byte [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 表單 顯示圖片 + 上傳


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲不卡免费视频 | 91日韩欧美| 久久艹精品 | 国产成人精品区在线观看 | 亚洲欧美人成人综合在线50p | 久久99精品九九九久久婷婷 | 你懂的国产 | 性做久久久久久久久25的美女 | 日本欧美韩国专区 | 国产精品久久亚洲不卡4k岛国 | 色偷偷亚洲 | 97影院理论片在线观看 | 久久美女| 天天干天天操天天玩 | 四虎影院观看 | 91一区二区三区四区五区 | 欧美日韩视频一区二区 | 成人老司机深夜福利久久 | 最新国产精品亚洲二区 | 美女视频很黄很暴黄是免费的 | 91精品全国免费观看 | 国产伦精品一区二区三区网站 | 污宅男666在线永久免费观看 | 热玖玖| 一级毛片免费网站 | 九九精品99久久久香蕉 | 在哪里可以看毛片 | 波多野结衣手机视频一区 | 日本免费一区二区三区看片 | 久久精品国产亚洲 | 欧美在线成人免费国产 | 俄罗斯毛片视频 | 牛牛影视在线 | 国产免费区 | 特级按摩一级毛片 | 久久国产一区二区 | 欧美高清国产 | 色综合久久88中文字幕 | 羞羞的视频网站 | 深夜精品寂寞在线观看黄网站 | 久久毛片免费看 |