<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 241,  comments - 116,  trackbacks - 0

    1 頁面文件
       <!-- Ext JS Files -->
    <link rel="stylesheet" type="text/css" href="/extjs4-file-upload-spring/extjs/resources/css/ext-all.css" />
        <script type="text/javascript" src="/extjs4-file-upload-spring/extjs/bootstrap.js"></script>

    <!-- file upload form -->
    <script src="/extjs4-file-upload-spring/js/file-upload.js"></script>

    </head>struts獲取各種表單的數(shù)據(jù)
    <body>

    Click on "Browse" button (image) to select a file and click on Upload button


    <div id="fi-form" style="padding:25px;"></div>
    </body>

    2 EXTjs的文件
       Ext.onReady(function(){

        Ext.create('Ext.form.Panel', {
            title: 'File Uploader',
            width: 400,
            bodyPadding: 10,
            frame: true,
            renderTo: 'fi-form',   
            items: [{
                xtype: 'filefield',
                name: 'file',
                fieldLabel: 'File',
                labelWidth: 50,
                msgTarget: 'side',
                allowBlank: false,
                anchor: '100%',
                buttonText: 'Select a File...'
            }],

            buttons: [{
                text: 'Upload',
                handler: function() {
                    var form = this.up('form').getForm();
                    if(form.isValid()){
                        form.submit({
                            url: 'upload.action',
                            waitMsg: 'Uploading your file...',
                            success: function(fp, o) {
                                Ext.Msg.alert('Success', 'Your file has been uploaded.');
                            }
                        });
                    }
                }
            }]
        });

    });

    3 上傳文件的bean
    import org.springframework.web.multipart.commons.CommonsMultipartFile;


    public class FileUploadBean {

        private CommonsMultipartFile file;

        public CommonsMultipartFile getFile() {
            return file;
        }

        public void setFile(CommonsMultipartFile file) {
            this.file = file;
        }
    }

     
    4 為了讓extjs顯示信息,再設計一個bean
    public class ExtJSFormResult {

        private boolean success;
        
        public boolean isSuccess() {
            return success;
        }
        public void setSuccess(boolean success) {
            this.success = success;
        }
        
        public String toString(){
            return "{success:"+this.success+"}";
        }
    }
      這里其實是返回是否成功

    5 controller層
      
    @Controller
    @RequestMapping(value = "/upload.action")
    public class FileUploadController {

        @RequestMapping(method = RequestMethod.POST)
        public @ResponseBody String create(FileUploadBean uploadItem, BindingResult result){

            ExtJSFormResult extjsFormResult = new ExtJSFormResult();
            
            if (result.hasErrors()){
                for(ObjectError error : result.getAllErrors()){
                    System.err.println("Error: " + error.getCode() +  " - " + error.getDefaultMessage());
                }
                
                //set extjs return - error
                extjsFormResult.setSuccess(false);
                
                return extjsFormResult.toString();
            }

            // Some type of file processing...
            System.err.println("-------------------------------------------");
            System.err.println("Test upload: " + uploadItem.getFile().getOriginalFilename());
            System.err.println("-------------------------------------------");
             if(uploadItem.getFile().getSize()>0){                   
                    try {    
                        SaveFileFromInputStream(uploadItem.getFile().getInputStream(),"D://",uploadItem.getFile().getOriginalFilename());    
                    } catch (IOException e) {    
                        System.out.println(e.getMessage());    
                        return null;    
                    }    
                }    
            //set extjs return - sucsess
            extjsFormResult.setSuccess(true);
            
            return extjsFormResult.toString();
        }
        
        /* **保存文件   
        
           * @param stream   
           * @param path   
           * @param filename   
           * @throws IOException   
           */   
          public void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException    
          {          
           FileOutputStream fs=new FileOutputStream(path + "/"+ filename);
           byte[]  buffer=new byte[1024*1024];
           int bytesum = 0;    
              int byteread = 0;
                while ((byteread=stream.read())!=-1)
                {
                    bytesum+=byteread;
                    
                      fs.write(buffer,0,byteread);    
                      fs.flush();    
                    
                }
                fs.close();    
                stream.close();    
          }    
      可以看到,當出現(xiàn)錯誤時,extjsFormResult.setSuccess(false);
    return extjsFormResult.toString();
      這兩句返回給前端ext js處理。
      最后就是配置MVC了
     
    <!-- Activates various annotations to be detected in bean classes -->
        <context:annotation-config />

        <!-- Scans the classpath of this application for @Components to deploy as beans -->
        <context:component-scan base-package="com.loiane"/>

        <!-- Configures Spring MVC -->
        <import resource="mvc-config.xml"/>

        <!-- Configure the multipart resolver -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- one of the properties available; the maximum file size in bytes -->
            <property name="maxUploadSize" value="100000"/>
          
        </bean>
      設置文件大小限制
      一個很奇怪的問題是,在ie 7下,好象有點問題,待解決,但在firefox和chrome下都沒問題,這個extjs 真怪,不用ext,普通的spring mvc是沒問題的哦
    posted on 2011-07-27 10:26 墻頭草 閱讀(2583) 評論(2)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     
    人人游戲網(wǎng) 軟件開發(fā)網(wǎng) 貨運專家
    主站蜘蛛池模板: 无码乱肉视频免费大全合集 | 精品亚洲麻豆1区2区3区| jizzjizz亚洲日本少妇| 处破痛哭A√18成年片免费| 亚洲另类视频在线观看| 在线看无码的免费网站| 亚洲第一精品电影网| 四虎在线免费视频| 国产成人亚洲精品| 成人性生免费视频| 在线观看亚洲专区| av在线亚洲欧洲日产一区二区| 久久高潮一级毛片免费| 久久久无码精品亚洲日韩蜜桃| 国产精品免费一区二区三区四区| 亚洲视频在线一区| 青青久在线视频免费观看| 亚洲AV色欲色欲WWW| 午夜亚洲国产成人不卡在线| 插鸡网站在线播放免费观看| 亚洲人成网www| 成人看的午夜免费毛片| 特级毛片aaaa免费观看| 久久国产精品亚洲综合| 成人免费毛片内射美女APP| 免费看一级毛片在线观看精品视频| 国产亚洲精久久久久久无码AV | 麻豆视频免费播放| 99亚偷拍自图区亚洲| 亚洲国产精品狼友中文久久久 | 国产亚洲精品资在线| 67pao强力打造高清免费| 亚洲国产欧美国产综合一区| 亚洲一区精品无码| 丁香花免费完整高清观看| 一本久久免费视频| 亚洲va精品中文字幕| 亚洲第一永久AV网站久久精品男人的天堂AV| a视频免费在线观看| 亚洲日本成本人观看| 亚洲AV无码国产精品麻豆天美|