Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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!");

}
Posted
Updated 3-Feb-16 0:06am
v2
Comments
Anisuzzaman Sumon 3-Feb-16 4:36am    
what is your markup(.aspx) code?show up your markup code
Member 10512842 3-Feb-16 6:01am    
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

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



<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:HiddenField ID="filepath" runat="server" />
</p>
<p>
<asp:Button ID="Button1" runat="server" Height="21px" OnClick="Button1_Click" Text="Convert" />
</p>
<p>
<asp:TextBox ID="TextBox2" runat="server" Height="184px" TextMode="MultiLine" Width="365px">
 <asp:Button ID="Button2" runat="server" Enabled="False" Height="21px" Text="Save As" />
</p>
<p>
 </p>
<p>
 </p>
Member 10512842 3-Feb-16 6:03am    
Actually I have to do is
1) One Textbox(for showing uploaded files path) and one button(for doing fuctionality of file upload control)


1 solution

You'd dynamically create the control like this

ASP.NET
<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

C#
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;
    }
}
 
Share this answer
 
Comments
Member 10512842 3-Feb-16 5:43am    
Thanks for reply,

Your code is preety good.

but I don't wanna to show fileupload control on page, directly on click of Upload button wanna to show File browser and after selection of file it gets upload.

Please guide.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900