<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebFileUpload.ascx.cs" Inherits="Solution.Admin.Controls.WebFileUpload" %>
<asp:TextBox ID="txtImagePath" AutoPostBack="false" AccessKey="x" runat="server" Width="288px" CssClass="text"></asp:TextBox>
<asp:Button ID="btnUploadFilePreview" OnClientClick="" runat="server" Text="预览" />
<br />
<asp:FileUpload ID="FileUploadSelect" runat="server" />
<asp:Button ID="btnUpload" OnClientClick="" runat="server" Text="上传" />
<iframe id="frameUploadFile" name="frameUploadFile" style="display: none;"></iframe>
<script type="text/javascript">
    document.getElementById('<%=btnUpload.ClientID %>').onclick=function()
    {
        return <%=btnUpload.ClientID %>.Upload(this);
        return false;
    };
    document.getElementById('<%=btnUploadFilePreview.ClientID %>').onclick=function()
    {
        <%=btnUpload.ClientID %>.Preview();
        return false;
    };
    var <%=btnUpload.ClientID %> = {
        UploadPath: '<%=UploadPath %>'+'&callback=<%=btnUpload.ClientID %>.CallbackMsg'+'&name='+document.getElementById("<%=FileUploadSelect.ClientID%>").name,
        ImgPathClientID: '<%=txtImagePath.ClientID %>',
        FileUploadClientID:'<%=FileUploadSelect.ClientID %>',
        previewRoot:'<%=PreviewRoot%>',
        uiobj:typeof(bx)!="undefined"?bx:null,
        FormObj: null,
        tempUploadFilePath: "",
        Upload: function (obj) {//上传
            this.FormObj = document.getElementsByTagName("form")[0];
            var uploadObj = document.getElementById(this.FileUploadClientID);
            if (uploadObj == null || uploadObj.value == "" || uploadObj.value == this.tempUploadFilePath) {
                alert("请选择文件");
                return false;
            }
            else {
                this.tempUploadFilePath = uploadObj.value;
            }
            this.FormObj.target = "frameUploadFile";
            this.FormObj.action = this.UploadPath;

            try {
                this.FormObj.submit();
            }
            catch (e) {
                alert("错误:" + e)
                this.FormObj.removeAttribute("target");
                this.FormObj.removeAttribute("action");
                return false;
            }
            return false;
        },
        CallbackMsg: function (msg) {//回调
            this.FormObj.removeAttribute("target"); 
            this.FormObj.removeAttribute("action");
            if (msg == null || msg.indexOf("/") == -1) {
                if (msg == "overrun") {
                    alert("上传的图片超过限制");
                    return;
                }
                if (msg == "noextension") {
                    alert("上传的图片格式不正确");
                    return;
                }
                else {
                    alert("上传图片的过程中失败了");
                    return;
                }
            }
            document.getElementById(this.ImgPathClientID).value = msg;
        },
        Preview:function(){
            if(this.uiobj!=null){
                var src=document.getElementById(this.ImgPathClientID).value;
                if(src.indexOf('/')!=0){src=this.previewRoot+src;}
                var content="<div style=\"max-width:450px;max-height:400px;overflow:auto;\"><img src=\""+src+"\"/></div>";
                this.uiobj.popup.messageBox({ "text": content, "background": true });
            }
        }
    }
</script>