简体   繁体   English

在asp.net中限制文件上传的大小

[英]Restrict the size of file upload in asp.net

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;
        }
    }
}

Look at this - How to increase the max upload file size in ASP.NET? 看看这个 - 如何增加ASP.NET中的最大上传文件大小?

You can change the max request size in web.config - the default is 4Mb 您可以在web.config中更改最大请求大小 - 默认值为4Mb

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. 默认情况下,ASP.NET仅允许将4,096千字节(KB)(或4 MB)或更少的文件上载到Web服务器。 To upload larger files, you must change the maxRequestLength parameter of the section in the Web.config file. 要上载较大的文件,必须更改Web.config文件中该部分的maxRequestLength参数。

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 . 注意在Machine.config文件中设置maxRequestLength属性,然后发布超过maxRequestLength值的请求(例如,文件上载)时,将无法显示自定义错误页面 Instead, Microsoft Internet Explorer will display a "Cannot find server or DNS" error message. 相反,Microsoft Internet Explorer将显示“无法找到服务器或DNS”错误消息。

The workaround is to modify the Machine.config file as explained in the answer given earlier by @sh1rts. 解决方法是修改Machine.config文件,如前面@ sh1rts给出的答案中所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM