Upload file using FileUpload Control
It is very common task to upload files to your website an example could be that you may want to save images of user who register to your website.
ASP.NET has given a control to upload files which is called FileUpload control. Below is an example how we can upload a file.
ASP.NET has given a control to upload files which is called FileUpload control. Below is an example how we can upload a file.
ASPX
<asp:FileUpload ID="fileUpload" runat="server" />
<asp:Button ID="btnUploadFile" runat="server" Text="Upload
Image" OnClick="btnUploadFile_Click" />
Code Behind
protected void btnUploadFile_Click(object sender, EventArgs e)
{
if (fileUpload.HasFile)
{
fileUpload.SaveAs(Server.MapPath("~/UploadedImages/" + fileUpload.FileName));
}
}
Above code will upload image in UploadedImages folder make sure folder already exists before you upload file or you can check existence of folder by using Directory.Exists() and create one if not already exists by using Directory.CreateDirectory(Server.MapPath("~/UploadedImages"));
Comments
Post a Comment