Active Server Pages (ASP) and Active Server Pages (ASP.NET) are server-side technologies for displaying interactive web pages.
ASP.NET offers developers a wide range of options in a large, diverse ecosystem of libraries and tools. Developers can also design custom libraries that can be shared with any .Net platform application.
ASP.NET also contains an authentication system for developers, which includes a database, libraries, templates for handling logins, external authentication to Google, Facebook, and other services, and more.
In detail, let us learn about one of the interesting web form controllers, i.e. ASP.NET FileUpload controllers.
ASP.NET FileUpload
The input controller ASP.NET FileUpload is used to upload files to the server. It adds a browse button to the form, which opens a window where you can select a file from your local machine.
We can drag FileUpload from the toolbox in Visual Studio to use it.
This is a server-side control that ASP.NET creates with its tag. The following is an example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CodingNinjasWebForms
{
public partial class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
{
string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
string SaveLocation = Server.MapPath("upload") + "\\" + fn;
try
{
FileUpload1.PostedFile.SaveAs(SaveLocation);
FileUploadStatus.Text = "The file has been uploaded.";
}
catch (Exception ex)
{
FileUploadStatus.Text = "Error: " + ex.Message;
}
}
else
{
FileUploadStatus.Text = "Please select a file to upload.";
}
}
}
}
As shown in the below screenshot, create a directory in the project to store submitted files.
When we run the code, we will get the following results.
We are uploading a file CodingNinjas.txt.
After uploading, it displays a successful file uploaded message, as shown in the screenshot below.
The file is stored in the upload directory. If you go within the folder, you'll see that the uploaded file is there.
Hence, with this, we learned to upload a file in ASP.NET.
FAQs
What is ASP.NET? Active Server Pages (ASP) and Active Server Pages (ASP.NET) are server-side technologies for displaying interactive web pages. ASP.NET offers developers a wide range of options in a large, diverse ecosystem of libraries and tools.
What is a web form? Visual Studio comes with ASP.NET Web Forms, which is part of the ASP.NET web application platform. ASP.NET Web Pages, ASP.NET MVC, and ASP.NET Single Page Apps are the other three programming paradigms you can use to construct ASP.NET web applications.
What are controls of the form? ASP.NET provides web forms controls, which are used to create HTML components. These controls are categorised as server-based and client-based. There are various server controls available for the web forms in ASP.NET: 1.Label 2.Checkbox 3.Text Box 4.Radio Button and many more.
How do we handle the submit request from the web form? The handling of the submit request is based on the trigger mechanism of the code behind the file, which triggers when the submit button is clicked on the web forms.
How do we run the user registration form? To run the web form, we just right click and select the view in the browser option in the code editor, to run the user registration form.
Key Takeaways
In this article, We learned about how to create a project to make a web form with ASP.NET FIleUpload controller. We learned about the properties of the ASP.NET FileUpload class. We also learned how to handle the submit request in the web form.