• webuploader项目中多文件上传实例


    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title> 百度uploader Demo</title>
    <!--引入CSS-->
    <link rel="stylesheet" type="text/css" href="/js/webuploader/css/webuploader.css">
    <script type="text/javascript" src="/js/webuploader/js/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="/js/webuploader/js/webuploader.min.js"></script>
    <!--SWF在初始化的时候指定,在后面将展示-->
    </head>
    <body>
    <h2>文件上传</h2>
    <!--dom结构部分-->
    <div id="uploader-demo">
        <!--用来存放item-->
        <div id="thelist" class="uploader-list"></div>
        <div class="btns">
    	<div id="picker"><input type="file" /></div> 		
    	<button id="ctlBtn" class="btn btn-default">开始上传</button> 			
        </div>	
    </div>
    
    <script type="text/javascript"> 
    jQuery(function() {
        var $ = jQuery,
            $list = $('#thelist'),
            $btn = $('#ctlBtn'),
            state = 'pending',
            uploader;
    
        uploader = WebUploader.create({
    
            // 不压缩image
            resize: false,
    
            // swf文件路径
            swf: '/js/webuploader/js/Uploader.swf',
    
            // 文件接收服务端。
            server: '/uploadMeterialAttachment.do',
            
            fileVal:'upload',
    
            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建,可能是input元素,也可能是flash.
            pick: '#picker'
        });
    
        // 当有文件添加进来的时候
        uploader.on( 'fileQueued', function( file ) {
            $list.append( '<div id="' + file.id + '" class="item">' +
                '<h4 class="info">' + file.name + '</h4>' +
                '<p class="state">等待上传...</p>' +
            '</div>' );
        });
    
        // 文件上传过程中创建进度条实时显示。
        uploader.on( 'uploadProgress', function( file, percentage ) {
            var $li = $( '#'+file.id ),
                $percent = $li.find('.progress .progress-bar');
    
            // 避免重复创建
            if ( !$percent.length ) {
                $percent = $('<div class="progress progress-striped active">' +
                  '<div class="progress-bar" role="progressbar" style=" 0%">' +
                  '</div>' +
                '</div>').appendTo( $li ).find('.progress-bar');
            }
            
            $li.find('p.state').text('上传中');
            $percent.css( 'width', percentage * 100 + '%' );
        });
    
        uploader.on( 'uploadSuccess', function( file,data ) {
            $( '#'+file.id ).find('p.state').text('已上传');
            $('#uploader-demo').append('<input  type="text" name="attachmentid" value="'+data.attachmentid+'"/>');   
            $( '#'+file.id ).addClass('upload-state-done');
        });
    
        uploader.on( 'uploadError', function( file ) {
            $( '#'+file.id ).find('p.state').text('上传出错');
        });
    
        uploader.on( 'uploadComplete', function( file ) {
            $( '#'+file.id ).find('.progress').fadeOut();
        });
    
        uploader.on( 'all', function( type ) {
            if ( type === 'startUpload' ) {
                state = 'uploading';
            } else if ( type === 'stopUpload' ) {
                state = 'paused';
            } else if ( type === 'uploadFinished' ) {
                state = 'done';
            }
    
            if ( state === 'uploading' ) {
                $btn.text('暂停上传');
            } else {
                $btn.text('开始上传');
            }
        });
    
        $btn.on( 'click', function() {
            if ( state === 'uploading' ) {
                uploader.stop();
            } else {
                uploader.upload();
            }
        });
        
        $("#aaa").click(attachmentids);	  //提交按钮
    });  
      
    </script>
    </body>
    </html>
    

    Struts配置中 result = "success"  type="json" 

    后台处理的action 方法:

    public String uploadMeterialAttachment() throws Exception {				
    		String path = ServletActionContext.getServletContext().getRealPath("upload");		
    		File file  = new File(path);
    		if (!file.exists()) {
    			file.mkdirs();
    		}		
    		List<String>  list = new ArrayList<String>();
    		String  filePath = ""; //文件保存路径
    		String  fileName = ""; //文件名称
    		for (int i = 0,l=upload.length;i<l; i++) {
    			fileName = uploadFileName[i];
    			File tmp =new File(file,System.nanoTime()+"_"+uploadFileName[i]);
    			FileUtils.copyFile(upload[i], tmp);
    			System.out.println(path+"/"+tmp.getName()); 
    			filePath=("/upload/"+tmp.getName());
    		}
    		
    		/**
    		 * 保存到附件表
    		 */
    		Attachment attachment = new Attachment();			
    		attachment.setFilename(fileName);
    		attachment.setFilepath(filePath);
    		Serializable ser = this.getBaseBean().saveObject(attachment);	
    		
    		if(ser != null ){
    			JSONObject jsonObject = new JSONObject();
    			jsonObject.put("attachmentid", ser);   		
    		    this.getPrintWriter(jsonObject);
    		}
    		
    		return Action.SUCCESS;
    	}
    

    将上传附件的 id 回调到前台,处理成一串字符串

    var attachmentids = $("input[name='attachmentid']").map(function(){return $(this).val()}).get().join(","); 
    

    后台处理这些ids  只需 用 string的split方法:

     

    	String[] ids = attachmentids.split(",");
    	    if(ids != null){
    	       for(String aid :ids){
    	        	Attachment attachment = (Attachment) bean.findObject(Attachment.class,Long.parseLong(aid));
    } }

      

     

     

     

  • 相关阅读:
    关于接口是值类型还是引用类型的猜测
    絮语工作四年的碎碎念
    烧钱游戏加入创业公司的一些感想
    关于C#调用非托管动态库方式的性能疑问
    couchbase作为分布式session容器时的注意事项
    poj3624
    明天的下载链接
    poj 1502
    poj1459 多源多汇最大流
    poj 3041
  • 原文地址:https://www.cnblogs.com/estellez/p/4530011.html
Copyright © 2020-2023  润新知