I am using FileUpload control in my ASP.NET application.
I want to set a file size limit of 1MB.
If the user uploads a file size greater than that limit. I want a warning message to be displayed to the user about file limit, instead of showing some exception in Web.config
public partial class UploadFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void uploadBtn_Click(object sender, EventArgs e)
{
if (FileUploadBtn.PostedFile.ContentLength > 1 * 1024 * 1024)
{
Message.Text = "File size has exceeded 1MB!";
Message.ForeColor = Color.Red;
return;
}
// save files
Message.Text = "File upload successfully!";
Message.ForeColor = Color.Green;
}
}
Here is the result.
Best Regards,
YihuiSun
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Is there a way I can configure it in web.config and use it for all the file upload controls? In my application there are too many file upload controls and using so much code for each and every control looks bit
Here is the result(This includes a demonstration of how to create user controls.).
Best Regards,
YihuiSun
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
67 Points
222 Posts
FileUpload setting maximum limit
Nov 11, 2020 11:47 AM|maverick786us|LINK
I am using FileUpload control in my ASP.NET application.
How can I accomplish this?
All-Star
194428 Points
28074 Posts
Moderator
Re: FileUpload setting maximum limit
Nov 11, 2020 03:03 PM|Mikesdotnetting|LINK
You can use the FileReader API to get the file size before the form is submitted:
var size = document.getElementById('myupload').files[0].size;
Contributor
2690 Points
772 Posts
Re: FileUpload setting maximum limit
Nov 12, 2020 03:11 AM|YihuiSun|LINK
Hi maverick786us,
You can limit the file size at the same time on the front and back ends, so that you can still limit the file size when the js code fails.
Here is an example, you can refer to it.
xxx.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadFile.aspx.cs" Inherits="DailyWebFormsDemo.UploadFile" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:FileUpload ID="FileUploadBtn" runat="server"/> <asp:Label ID="Message" runat="server"></asp:Label> <asp:Button ID="uploadBtn" runat="server" Text="upload" OnClick="uploadBtn_Click"/> </form> <script src="Scripts/jquery-3.4.1.min.js"></script> <script> $("#uploadBtn").click(function () { if ($("#FileUploadBtn")[0].files[0].size> (1 * 1024 * 1024)) { $("#Message").css("color", "red"); $("#Message").html("File size has exceeded 1MB!"); $("#FileUploadBtn").val(""); return false; } }); </script> </body> </html>
xxx.aspx.cs
public partial class UploadFile : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public void uploadBtn_Click(object sender, EventArgs e) { if (FileUploadBtn.PostedFile.ContentLength > 1 * 1024 * 1024) { Message.Text = "File size has exceeded 1MB!"; Message.ForeColor = Color.Red; return; } // save files Message.Text = "File upload successfully!"; Message.ForeColor = Color.Green; } }
Here is the result.
Best Regards,
YihuiSun
Member
67 Points
222 Posts
Re: FileUpload setting maximum limit
Nov 12, 2020 11:11 AM|maverick786us|LINK
Thank you.
Is there a way I can configure it in web.config and use it for all the file upload controls? In my application there are too many file upload controls and using so much code for each and every control looks bit
Contributor
2690 Points
772 Posts
Re: FileUpload setting maximum limit
Nov 20, 2020 07:30 AM|YihuiSun|LINK
Hi maverick786us,
<httpRuntime maxRequestLength="xxx"/>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DailyWebformsDemo.Test.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <CustomUserControl:CustomUploadUserControl runat="server" /> </form> </body> </html>
Here is the result(This includes a demonstration of how to create user controls.).
Best Regards,
YihuiSun