File upload error handling
FileUploadErrorHandle.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
try
{
string uploadFolder = Request.PhysicalApplicationPath + "Upload\\";
FileUpload1.SaveAs(uploadFolder + FileUpload1.FileName);
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = "File uploaded successfully: " + FileUpload1.PostedFile.FileName;
}
catch(Exception ex)
{
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = "an error occured.<br />";
Label1.Text += ex.Message;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net FileUpload example: how to check (handle) error when upload a file</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy">asp.net FileUpload example: FileUpload Error Check</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Larger"
Font-Italic="true"
Font-Bold="true"
>
</asp:Label>
<br /><br />
<asp:Label
ID="Label2"
runat="server"
ForeColor="HotPink"
Text="Choose a file for upload it."
Font-Bold="true"
>
</asp:Label>
<br />
<asp:FileUpload
ID="FileUpload1"
runat="server"
BackColor="HotPink"
ForeColor="AliceBlue"
/>
<asp:Button
ID="Button1"
runat="server"
Font-Bold="true"
ForeColor="HotPink"
OnClick="Button1_Click"
Text="Upload Now"
/>
<br /><br />
</div>
</form>
</body>
</html>
- asp.net FileUpload example: how to upload file with specific extension
- asp.net FileUpload validation example: how to validate FileUpload control (file upload)
- asp.net FileUpload example: how to check whether FileUpload control has file
- asp.net FileUpload example: how to get posted file full name after upload a file
- asp.net FileUpload example: how to get posted file content length (size) after upload file
- asp.net FileUpload example: how to get posted file content type after upload file
- asp.net FileUpload example: how to rename file when upload (change file name when upload)
- FileUpload example: how to use FileUpload control in asp.net