<%@ Page Language="C#" AutoEventWireup="true" Inherits="Kesion.NET.WebSite.User.Include.UploadAttachment" Codebehind="UploadAttachment.aspx.cs" %>
<%@ Import Namespace="Kesion.Publics" %>
<%@ Import Namespace="Kesion.Cache" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title></title>
    <link href="/admin/style/Admin_Style.CSS" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../ks_inc/jquery.js"></script>
 <script type="text/javascript" src="/ks_inc/webuploader/js/webuploader.min.js"></script>
 <link rel="stylesheet" type="text/css" href="/ks_inc/webuploader/css/webuploader.css">
 <link rel="stylesheet" type="text/css" href="/ks_inc/webuploader/css/style.css">

<script type="text/javascript">
    var basictype = 1;
	</script>

</head>
<body>
    <form id="form1" runat="server">
    
        <div id="uploadfile">
     <table cellspacing="0" cellpadding="0">
	 <tr>
	  <td width="500"><div id="thelist" class="uploader-list"></div>
                <div class="form-group form-inline">
                    <div id="picker" style="float:left">选择文件</div> &nbsp;
                    <button id="ctlBtn" class="btn btn-default" style="padding:8px 15px;display:none">开始上传</button>
                    单文件限制大小 <font color=red><%=limitSize>=1024? Math.Round((double)(limitSize/1024),2).ToString() +" MB":limitSize+" KB"%></font>
                </div>
	  </td>
	 
	</tr>
	</table>
    </div>

        <script>
            $(function(){
                var $ = jQuery,
                   $list = $('#thelist'),
                    $btn = $('#ctlBtn'),
                   state = 'pending',
                   uploader;

                   uploader = WebUploader.create({
                    // 不压缩image
                    resize: false,
                    swf: '/ks_inc/webuploader/js/uploader.swf', // swf文件路径
                    pick: '#picker', // 选择文件的按钮。可选
                   // chunked: true, //是否要分片处理大文件上传
                  //  chunkSize:2*1024*1024, //分片上传，每片2M，默认是5M
                    fileSingleSizeLimit: <%=limitSize %>*1024, //限制单个上传图片的大小

                     <%if (KSCMS.S("upfrom")=="Admin"){ %>
                       server: "<%=KSCMS.GetInstallDir() %><%=MyCache.GetCacheConfig(4) %>/Include/uploads.ashx",
                       formData: { "AdminLoginCode":"<%=new TemporaryVar().AdminLoginCode %>", "adminuser":"<%=new TemporaryVar().AdminName %>","adminpass":"<%=new TemporaryVar().AdminPass %>","PowerList":"<%=Kesion.BLL.KSSecurity.ReturnPowerList() %>","usertype":"<%=new TemporaryVar().AdminUserType %>","EditorID":"<%=KSCMS.S("EditorID") %>","upBySwf": "1", "FieldID":"-1","AddWaterFlag": "1",  UpType: "File", BasicType: <%=basicType %>, ChannelID: <%=channelId %>, AutoRename: 4 }
                    <%}else{ %>
                        server: "Uploads.ashx",
                    formData: {  "username":"<%=new TemporaryVar().UserName %>","password":"<%=new TemporaryVar().PassWord %>","EditorID":"<%=KSCMS.S("EditorID") %>","upBySwf": "1", "FieldID":"-1","AddWaterFlag": "1",  UpType: "File", BasicType: <%=basicType %>, ChannelID: <%=channelId %>, AutoRename: 4 }
                    <%} %>



                });
                // 当有文件被添加进队列的时候
                uploader.on( 'fileQueued', function( file ) {
                    $list.append( '<div id="' + file.id + '" class="item">' +
                        '<h4 class="info" style="display:none">' + file.name + '</h4>' +
                        '<p class="state" style="display:none">等待上传...</p>' +
                    '</div>' );

                    //触发上传
                    if (state === 'uploading') {
                        uploader.stop();
                    } else {
                        uploader.upload();
                    }
                });
                // 文件上传过程中创建进度条实时显示。
                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="width: 0%">' +
                          '</div>' +
                        '</div>').appendTo( $li ).find('.progress-bar');
                    }

                    $li.find('p.state').text('上传中');

                    $percent.css( 'width', percentage * 100 + '%' );
                });
                // 文件上传成功
                uploader.on( 'uploadSuccess', function( file,response ) {
                    if (response.status==0){
                        alert(response.error);
                        return;
                    }
                    var serverData=response.url;
                    <%if (channelId==20000){%>
                    var d=unescape(serverData).split('|');
                    parent.showAskPic(serverData);
                    <%}else{%>
                    var d=unescape(serverData).split('|');
                    if (basictype!=99999){ parent.InsertFileFromUp(d[0],file.size,d[1],file.name,d[2])}
                    <%}%>

                    $( '#'+file.id ).find('p.state').text('已上传');
                });
            uploader.on('error', function (code) {
                var err = '';
                switch (code) {
                    case 'F_EXCEED_SIZE':
                        err += '单个文件大小不得超过' +  uploader.options.fileSingleSizeLimit + 'kb！';
                        break;
                    case 'Q_EXCEED_NUM_LIMIT':
                        err += '最多只能上传' +  uploader.options.fileNumLimit + '张！';
                        break;
                    case 'Q_EXCEED_SIZE_LIMIT':
                        err += '上传总大小超出'+limitSize;
                        break;
                    case 'Q_TYPE_DENIED':
                        err += '无效图片类型，请上传正确的图片格式！';
                        break;
                    case 'F_DUPLICATE':
                        err+='不能重复上传相同文件！' ;
                        break;
                    default:
                        err += '上传错误，请刷新重试！错误代码：'+code;
                        break;
                }
                alert(err);
                return false;
            });


                // 文件上传失败，显示上传出错
            uploader.on( 'uploadError', function( file ) {
                    $( '#'+file.id ).find('p.state').text('上传出错');
                });
                // 完成上传完
                uploader.on( 'uploadComplete', function( file,response ) {
                    $( '#'+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';
                    }

                });

               
            });
        </script>
      



    </form>
</body>
</html>
