ASP.NET - File Upload
ASP.NET is a web development framework created by Microsoft that allows developers to build dynamic web applications. One common feature that developers often need to implement in their applications is file uploading.
To allow users to upload files using an ASP.NET application, you can use the FileUpload control. Here's an example of how you can use the FileUpload control in a web form:
<%@ Page Language="C#" %>
<!DOCTYPE html>
<html>
<head>
<title>File Upload Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
In the above code, we have a web form that contains a FileUpload control and a button. When the user selects a file using the FileUpload control and clicks the button, the file will be uploaded to the server. Here's the code for the button's click event:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/uploads/" + filename));
Response.Redirect(Request.Url.AbsoluteUri);
}
}
In the above code, we first check if a file has been selected using the FileUpload control's HasFile property. If a file has been selected, we get the file name using the Path.GetFileName method and save the file to the server using the SaveAs method.
Note that in this example, we're saving the file to a directory called "uploads" in the root of the application. You'll need to make sure that this directory exists and that your ASP.NET application has permission to write to it.
Here's an example that includes some basic validation for the uploaded file:
<%@ Page Language="C#" %>
<!DOCTYPE html>
<html>
<head>
<title>File Upload Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Label ID="lblError" runat="server" ForeColor="Red" Visible="false" />
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
In this example, we've added a label control that we'll use to display any validation errors. Here's the code for the button's click event:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
// Check file size
int fileSize = FileUpload1.PostedFile.ContentLength;
if (fileSize > 5000000) // 5 MB
{
lblError.Visible = true;
lblError.Text = "File size cannot exceed 5 MB";
return;
}
// Check file type
string fileType = FileUpload1.PostedFile.ContentType;
if (fileType != "image/jpeg" && fileType != "image/png")
{
lblError.Visible = true;
lblError.Text = "Only JPEG and PNG files are allowed";
return;
}
// Save file
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/uploads/" + filename));
Response.Redirect(Request.Url.AbsoluteUri);
}
else
{
lblError.Visible = true;
lblError.Text = "Please select a file to upload";
}
}
In this code, we've added two validation checks. The first checks the size of the uploaded file and makes sure it's less than or equal to 5 MB. The second checks the file type and makes sure it's either a JPEG or PNG image. If either of these checks fail, we display an error message in the label control and return without saving the file.