注册 登录
编程论坛 ASP.NET技术论坛

asp.net FileUpLoad控件总是上传不了

上帝之允 发布于 2012-04-10 21:07, 991 次点击
<asp:FileUpload ID="FU" runat="server" />
<asp:ImageButton ID="upload" runat="server" ImageUrl="~/ShowLove_image/upload.gif" CausesValidation="false"
           Height="26px" Width="94px" onclick="upload_Click" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FU"
            ErrorMessage="必须是 jpg或者gif文件" ValidationExpression="^(([a-zA-Z]:)|(\\{2}\W+)\$?)(\\(\W[\W].*))+(.jpg|.Jpg|.gif|.Gif)$"></asp:RegularExpressionValidator>

后台:
  protected void upload_Click(object sender, ImageClickEventArgs e)
    {
        if (FU.HasFile)
        {
            try
            {
                string serverPath = Server.MapPath("ShowLove_image");
                string imgName = FU.FileName;
                string NewPath = serverPath + "\\" + imgName;
                FU.SaveAs(NewPath);
                ScriptManager.RegisterStartupScript(UP, this.GetType(), "alert", "alert('上传成功!')", true);
                detail.Text = "";
                receiver.ImageUrl = "ShowLove_image/" + imgName;
                detail.Text += " 路径 : " + FU.PostedFile.FileName + "\n" + " 大小 :" + FU.PostedFile.ContentLength / 1024 + "\n" + " 类型 :" + FU.PostedFile.ContentType;
            }
            catch
            {
                ScriptManager.RegisterStartupScript(UP, this.GetType(), "alert_1", "alert('上传失败!')", true);
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(UP, this.GetType(), "alert_2", "alert('请选择图片!')", true);
        }
    }


总是通过不了验证控件,显示不是jpg文件,取下控件之后,通过不了hasfile方法,不知道什么原因导致了这个问题,麻烦各位了!!!
2 回复
#2
上帝之允2012-04-10 22:12
回复 楼主 上帝之允
知道了,是ajax的问题
#3
qiwa2012-05-23 07:34
学习了
1