webuploader批量上傳

這是前臺界面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/webuploader.css" />
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/webuploader.min.js"></script>
</head>
 <h3>圖片上傳</h3>
        <!--dom結構部分-->
    <div id="uploader-demo">
        <!--用來存放item-->
        <div id="fileList" class="uploader-list"></div>
        <div id="upInfo" ></div>
        <div id="filePicker">選擇文件</div>
    </div>
    <input type="button" id="btn" value="開始上傳">

<script>
// 圖片上傳demo
jQuery(function() {
    var $ = jQuery,
        $list = $('#fileList'),
        // 優化retina, 在retina下這個值是2
        ratio = window.devicePixelRatio || 1,
        // 縮略圖大小
        thumbnailWidth = 100 * ratio,
        thumbnailHeight = 100 * ratio,
        // Web Uploader實例
        uploader;
        // 初始化Web Uploader
        uploader = WebUploader.create({
        // 自動上傳。
        auto: false,
        // swf文件路徑
        swf:'../js/Uploader.swf',
        // 文件接收服務端。使用spring對應後臺action
        server: 'uploader',
        threads:'5',        //同時運行5個線程傳輸
        fileNumLimit:'10',  //文件總數量只能選擇10個

        // 選擇文件的按鈕。可選。
        pick: {id:'#filePicker',  //選擇文件的按鈕
                multiple:true},   //允許可以同時選擇多個圖片
     // 圖片質量,只有type爲`image/jpeg`的時候纔有效。
        quality: 90,

        //限制傳輸文件類型,accept可以不寫
        accept: {
            title: 'Images',//描述
            extensions: 'jpg,jpeg,bmp,png',//類型
            mimeTypes: 'image/*'//mime類型
        }
    });


 // 當有文件添加進來的時候,創建img顯示縮略圖使用
    uploader.on( 'fileQueued', function( file ) {
        var $li = $(
                '<div id="' + file.id + '" class="file-item thumbnail">' +
                    '<img>' +
                    '<div class="info">' + file.name + '</div>' +
                '</div>'
                ),
            $img = $li.find('img');

        // $list爲容器jQuery實例
        $list.append( $li );

        // 創建縮略圖
        // 如果爲非圖片文件,可以不用調用此方法。
        // thumbnailWidth x thumbnailHeight 爲 100 x 100
        uploader.makeThumb( file, function( error, src ) {
            if ( error ) {
                $img.replaceWith('<span>不能預覽</span>');
                return;
            }

            $img.attr( 'src', src );
        }, thumbnailWidth, thumbnailHeight );
    });

 // 文件上傳過程中創建進度條實時顯示。    uploadProgress事件:上傳過程中觸發,攜帶上傳進度。 file文件對象 percentage傳輸進度 Nuber類型
    uploader.on( 'uploadProgress', function( file, percentage ) {
        var $li = $( '#'+file.id ),
            $percent = $li.find('.progress span');

        // 避免重複創建
        if ( !$percent.length ) {
            $percent = $('<p class="progress"><span></span></p>')
                    .appendTo( $li )
                    .find('span');
        }

        $percent.css( 'width', percentage * 100 + '%' );
    });

    // 文件上傳成功時候觸發,給item添加成功class, 用樣式標記上傳成功。 file:文件對象,    response:服務器返回數據
    uploader.on( 'uploadSuccess', function( file,response) {
        $( '#'+file.id ).addClass('upload-state-done');
        //console.info(response);
      $("#upInfo").html("<font color='red'>"+response._raw+"</font>");
    });

    // 文件上傳失敗                                file:文件對象 , code:出錯代碼
    uploader.on( 'uploadError', function(file,code) {
        var $li = $( '#'+file.id ),
            $error = $li.find('div.error');

        // 避免重複創建
        if ( !$error.length ) {
            $error = $('<div class="error"></div>').appendTo( $li );
        }

        $error.text('上傳失敗!');
    });

    // 不管成功或者失敗,文件上傳完成時觸發。 file: 文件對象
    uploader.on( 'uploadComplete', function( file ) {
        $( '#'+file.id ).find('.progress').remove();
    });

    //綁定提交事件
    $("#btn").click(function() {
        console.log("上傳...");
        uploader.upload();   //執行手動提交
        console.log("上傳成功");
      });

});
</script>
</body>
</html>

這是webuploader.css

.webuploader-container {
	position: relative;
}
.webuploader-element-invisible {
	position: absolute !important;
	clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
    clip: rect(1px,1px,1px,1px);
}
.webuploader-pick {
	position: relative;
	display: inline-block;
	cursor: pointer;
	background: #00b7ee;
	padding: 10px 15px;
	color: #fff;
	text-align: center;
	border-radius: 3px;
	overflow: hidden;
}
.webuploader-pick-hover {
	background: #00a2d4;
}

.webuploader-pick-disable {
	opacity: 0.6;
	pointer-events:none;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章