在click事件上调用隐藏的FileUpload控件

时间:2015-04-27 07:42:14

标签: c# asp.net file-upload buttonclick

我的asp.net页面上有一个隐藏的FileUpload控件

<asp:FileUpload id="FileUploadControl" runat="server" CssClass="hidden" />

我希望在用户点击Browse按钮

时调用此控件
<asp:Button runat="server" id="BrowseButton" text="browse" />

每次用户点击FileUpload按钮时,是否可以控制browse控件?

编辑:

BrowseButton_Click事件中,我想上传文件

 protected void BrowseButton_Click(object sender, EventArgs e)
        {
            if (FileUploadControl.HasFile)
            {
                try
                {
                    string filename = Path.GetFileName(FileUploadControl.FileName);
                    FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                    Response.Write("<script>alert('done');</script>");
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }

            }
            else
            {
                Response.Write("<script>alert('please choose');</script>");
            }
        }

1 个答案:

答案 0 :(得分:3)

是的,您可以使用jQuery在客户端执行此操作。请找到以下代码:

<asp:FileUpload id="FileUploadControl" runat="server"  CssClass="hidden" />

<asp:Button runat="server" id="BrowseButton" text="browse" OnClientClick="openfileDialog();"/>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script>
   function openfileDialog() {
       $("#FileUploadControl").click();
   }
</script>