Restrict the size of file upload in asp.net
Asked Answered
A

2

5

I want to restrict my file size upload to a certain limit. But, the problem here is that I want to provide a popup alert when the upload size is exceeded . But , instead the web page here shows the following error

HTTP Error 404.13 - Not Found

The request filtering module is configured to deny a request that exceeds the request content length. Here's my code

 protected void Button1_Click(object sender, EventArgs e)
{
    if (fuDocpath.HasFiles)
    {
        try
        {
            DateTime now = DateTime.Now;
            lbldateStamp.Text = now.ToString("mm_dd_yyyy_hh_mm_ss");
            //string foldername = lblsessionID.Text + "_" + lbldateStamp.Text;
            string folderpath = (Server.MapPath("~/Uploaded_Files/") + lblsessionID.Text + "_" + lbldateStamp.Text + "/");
            Directory.CreateDirectory(folderpath);
            if (fuDocpath.PostedFile.ContentLength < 20970000)
            {
                try
                {
                    foreach (HttpPostedFile files in fuDocpath.PostedFiles)
                    {
                        string filename = Path.GetFileName(files.FileName);
                        string folderpath1 = folderpath + "/";
                        fuDocpath.SaveAs(folderpath1 + filename);
                        lblName.Text = lblName.Text + "|" + filename;
                        lblerror.Text = string.Empty;
                    }
                }
                catch (Exception eex)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + eex.Message + "')", true);
                }

            }
        }
        catch (Exception ex)
        {
            lblerror.Text = "File couldn't be uploaded." + ex.Message;
            lblName.Text = string.Empty;
        }
    }
}
Ansel answered 4/7, 2014 at 5:12 Comment(6)
what is the total file size you want to upload?Pylos
anywhere between 10 to 15 MB . Iam actually creating a user based applications . So, clients will upload files of total maximum size 15 MBAnsel
your code works fine....i didn't get any single error.Can you provide us what actually u r doing when this error pops OR the steps to reproduce this issuePylos
just try to upload anything larger than 5 MB and it won;t show popup alertAnsel
Could you use jquery??? It would be easy checking file size and showing pop alert in jquerySeem
I don't know jquery ,otherwise I would have tagged it.Ansel
M
6

Look at this - How to increase the max upload file size in ASP.NET?

You can change the max request size in web.config - the default is 4Mb

Maggie answered 4/7, 2014 at 5:53 Comment(2)
that's fine But what if I want to show popup alert it anything larger than is uploadedAnsel
The you'll need to use client-side validation. This could help - codeproject.com/Tips/290098/… - it's a custom validator for FileUpload controls that does basically what you're trying to doMaggie
D
1

Just in case you're wondering about the nature of the error code/message thrown by the browser, here is a post that addresses it. I'll quote a paragraph just in case of broken links in future:

By default, ASP.NET only permits files that are 4,096 kilobytes (KB) (or 4 MB) or less to be uploaded to the Web server. To upload larger files, you must change the maxRequestLength parameter of the section in the Web.config file.

Note When the maxRequestLength attribute is set in the Machine.config file and then a request is posted (for example, a file upload) that exceeds the value of maxRequestLength, a custom error page cannot be displayed. Instead, Microsoft Internet Explorer will display a "Cannot find server or DNS" error message.

The workaround is to modify the Machine.config file as explained in the answer given earlier by @sh1rts.

Danube answered 14/3, 2017 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.