We can use the following code to restrict the File upload size using Asp.Net, C#.
We can set the maximum upload limit for the file, using maxRequestLength in web.config of your ASP.NET web application.
Here is the sample code for this...
protected void btnUpload_Click(object sender, System.EventArgs e)
{
string fileName = FileSelector.PostedFile.FileName;
int fN = fileName.LastIndexOf("\\");
string newFileName = fileName.Substring(fN).ToLower();
if (newFileName.EndsWith(".m4v") || newFileName.EndsWith(".mp3") || newFileName.EndsWith(".mp4") || newFileName.EndsWith(".mov"))
{
if (this.FileSelector.PostedFile.ContentLength < 210000)
{
FileSelector.PostedFile.SaveAs(
Server.MapPath("../audioFiles/") + newFileName);
lblWarn.Text = "File uploaded";
lblWarn.ForeColor = System.Drawing.Color.Green;
lblWarn.Visible = true;
}
else
{
lblWarn.Text = "File cannot exceed 350MB";
lblWarn.Visible = true;
lblWarn.ForeColor = System.Drawing.Color.Red;
}
}
else
{
lblWarn.Text = "Only extensions .m4v, .mp4, mp3, or .mov";
lblWarn.Visible = true;
lblWarn.ForeColor = System.Drawing.Color.Red;
}
}
Also default upload limit is 4MB in asp.net. You can change this value in ur web.config file.
<httpRuntime maxRequestLength="210000"/> Include this in your webconfig file in the system.web section You can also use FileUpload1.PostedFile.ContentLength to calculate the length of the file after it is uploaded to server.
No comments:
Post a Comment