解决:上传文件按钮样式,fileupload 样式,同时解决后台得不到上传文件的信息问题

本文档介绍了如何解决在使用fileupload组件时遇到的样式问题,包括按钮边框显示异常和服务器无法获取上传文件信息的常见错误。通过调整CSS样式和过滤器设置,确保上传文件按钮的外观正常,并且后端能够正确接收并处理上传的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//前台代码<style>.green_btn{
 background-color:#79a500;
 }</style> <form runat="server" id="form1"></form><input id="txtUploadfile" type="text" /><asp:FileUpload ID="uploadfile" runat="server" Style="filter: alpha(opacity=0); width: 70px;
cursor: hand; position: absolute; height: auto;" onchange="setValue()" />
<input type="button" id="SearchFile" value="Browse.." class="green_btn" style="width: 70px;" />
<asp:Button ID="btnSearch" runat="server" Text="upload" CssClass="green_btn" OnClick="btnSearch_Click" />
<script type="text/javascript" language="javascript">//得到文件的路径,解析出fakepath路径的原始值
    function getPath(obj) {
        if (obj) {
            if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
                obj.select();
                return document.selection.createRange().text;
            } else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
                if (obj.files) {
                    return obj.files.item(0).getAsDataURL();
                }
                return obj.value;
            }
            return obj.value;
        }
    }//将上传文件的文本框值赋给txtUploadfile    function setValue() {
        document.getElementById("txtUploadfile").value = getPath(document.getElementById("uploadfile"));
    }
        
</script>            


 
protected void btnSearch_Click(object sender, EventArgs e) { try {

//将高度设置为0,保证在后台处理这段时间,前台的上传按钮样式不会出来     
            uploadfile.Style.Value = "filter: alpha(opacity=0); width: 70px;cursor: hand; position:absolute; height:0px; vertical-align:top;-moz-focus-inner:border:none;";

            Stream fs = uploadfile.PostedFile.InputStream;
            int DataLen = uploadfile.PostedFile.ContentLength;
            string Type = uploadfile.PostedFile.ContentType;
            string filename = Path.GetFileName(uploadfile.PostedFile.FileName);
            string filetype = Path.GetExtension(uploadfile.PostedFile.FileName);

            byte[] Data = new byte[DataLen];
            fs.Read(Data, 0, DataLen);

            fs.Close();
        }
        catch (Exception ex)
        {
            string message = ex.Message;
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "<script language='javascript'>alert('" + message + "')</script>", false);
        }
    }

 public void Page_PreRender(object sender, EventArgs e)
    {//这里恢复按钮的高度,显示出来
        uploadfile.Style.Value = @"filter: alpha(opacity=0); width: 70px;cursor: hand; position:absolute; 
        height:20px; vertical-align:top;-moz-focus-inner:border:none; ";
    }

 

 




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值