With the File Upload conrol we can allow user to upload file to the server.
<asp:FileUpload ID="FileUpload1" runat="server" />
Code:
<asp:FileUpload ID="FileUpload1" runat="server" ondatabinding="Button1_Click" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />
<asp:Label ID="Label1" runat="server"></asp:Label>
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
// HasFile check that there is any file available for uploading or not.
{
FileUpload1.SaveAs(Server.MapPath("~/NewFolder1/" +FileUpload1.FileName));
Label1.Text = "File Uploaded";
}
else
Label1.Text = "You have not specified a file";
}
The Server.MapPath method maps a specified path to a physical path. In ASP.NET the ~ tilde indicates the root of a virtual path.
Run The Project
Now browse and select file and click upload button then it will save the file in NewFolder1.
When you click Upload button then HasFile property of FileUpload control check that FileUpload control contain a file or not If there is a file then control will go for the next step (uploading) otherwise it will not.
We can change its appearance
<asp:FileUpload ID="FileUpload1" runat="server" Font-Bold="True"
Font-Italic="True" Font-Names="Arial Black" ForeColor="Red" />
Leave Comment
2 Comments
View All Comments