With ASP.NET, accepting file uploads from users has very easy. With the FileUpload control, it is possible with a small amount of code lines, check the following example.
Code:
<form id="form1" runat="server">
<asp:FileUpload id="FileUploadControl" runat="server" />
<asp:Button runat="server" id="btn_Upload" text="Upload" onclick="UploadButton_Click" />
<br /><br />
<asp:Label runat="server" id="lbl_Status" text="Upload status: " />
</form> And here is the CodeBehind code required to handle the upload:
Code:
protected void btn_Upload_Click(object sender, EventArgs e)
{
if(FileUploadControl.HasFile)
{
try
{
string filename = Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
lbl_Status.Text = “File uploaded successfully!";
}
catch(Exception ex)
{
lbl_Status.Text = “The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
Bookmarks