ASP.NET controls should rather be placed in aspx markup file. That is the preferred way of working with them. So add FileUpload
control to your page. Make sure it has all required attributes including ID
and runat
:
<asp:FileUpload ID="FileUpload1" runat="server" />
Instance of FileUpload1
will be automatically created in auto-generated/updated *.designer.cs file which is a partial class for your page. You usually do not have to care about what's in it, just assume that any control on an aspx page is automatically instantiated.
Add a button that will do the post back:
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
Then go to your *.aspx.cs file where you have your code and add button click handler. In C# it looks like this:
protected void Button1_Click(object sender, EventArgs e)
{
if (this.FileUpload1.HasFile)
{
this.FileUpload1.SaveAs("c:" + this.FileUpload1.FileName);
}
}
And that's it. All should work as expected.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…