如何向按钮提供文件上传控件的功能?我尝试下面的代码,但点击按钮文件浏览器不显示。 [英] How do I can provide functionality of a file upload control to button? I tried below code but on click of button file browser not showing.

查看:84
本文介绍了如何向按钮提供文件上传控件的功能?我尝试下面的代码,但点击按钮文件浏览器不显示。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Button3_Click(object sender, EventArgs e)
{
    // Initializes a OpenFileDialog instance


    FileUpload fu = new FileUpload();
    string filename = Path.GetFileName(fu.FileName);
    fu.SaveAs(Server.MapPath("~/Excel/") + filename);
    MessageBox("Upload status: File uploaded!");

}

推荐答案

你可以像这样动态创建控件



You'd dynamically create the control like this

<asp:Panel ID="PanelContainer" runat="server">

</asp:Panel>

<asp:Button ID="ButtonUpload" runat="server" OnClick="ButtonUpload_Click" Text="Upload" />

<asp:Literal ID="LiteralMessage" runat="server" Visible="false" EnableViewState="false"><p>File uploaded</p></asp:Literal>





代码隐藏





code-behind

protected void Page_Load(object sender, EventArgs e)
{
    FileUpload fu = new FileUpload();
    fu.ID = "MyFileUpload";
    PanelContainer.Controls.Add(fu);
}        

protected void ButtonUpload_Click(object sender, EventArgs e)
{
    FileUpload fu = PanelContainer.FindControl("MyFileUpload") as FileUpload;

    if (fu != null && fu.HasFile)
    {
        fu.SaveAs(Server.MapPath(string.Concat("~/app_data/", fu.FileName)));

        LiteralMessage.Visible = true;
    }
}


这篇关于如何向按钮提供文件上传控件的功能?我尝试下面的代码,但点击按钮文件浏览器不显示。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆