我的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>");
}
}
答案 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>