如何在ASP.NET C#上传时验证文件的大小?

时间:2012-09-18 21:14:40

标签: c# jquery asp.net validation upload

我正在开发一个发送电子邮件的模块,其中一项功能是上传多个文件......

我用它作为创建它的参考: http://www.dotnetcurry.com/ShowArticle.aspx?ID=68

我还必须添加jquery以允许它上传多个文件。

<script src="Scripts/jquery-1.3.2.js" type="text/javascript">
</script>

<script src="Scripts/jquery.MultiFile.js" type="text/javascript">
</script>

但是,如果我在表单中按下按钮时上传大于10MB的文件(我想只要发生回发就会发生这种情况),浏览器无法加载页面。所以我想要的是在我的按钮中触发这个功能:

protected void imgBtnEnviar_Click(object sender, ImageClickEventArgs e)
{
    int intBytes = 0;
    string strArchivos = "";
    string strRutaArchivos = "";
    bool blnBytes = true;
    bool blnPermitir = true;

    try
    {
        HttpFileCollection hfc = Request.Files;

        //Check if all files together are less than 10mb
        foreach (HttpPostedFile hpf in hfc)
        {
            intBytes += hpf.ContentLength;

            if (intBytes > 11000000)
            {
                blnBytes = false;
                break;
            }
        }

        if (blnBytes)
        {
            //Upload All attached files to server
            foreach (HttpPostedFile hpf in hfc)
            {
                if (hpf.FileName == "")
                {
                    break;
                }

                string strNombre = ((int)Session["Grupo"]) + "_" + (int)Session["EmailIdCliente"] + "_" + DateTime.Now.ToString("MM-dd-yyyy_HH.mm.ss") + "_" + Path.GetFileName(hpf.FileName);
                strArchivos += strNombre + ";";
                strRutaArchivos += Server.MapPath("~/Archivos/") + strNombre + ";";
                hpf.SaveAs(Server.MapPath("~/Archivos/") + strNombre);
            }
        }
    }

    catch
    {
        blnPermitir = false;
    }

    if (!blnBytes || !blnPermitir)
    {
        //Error Message Displayed To User
        ClientScript.RegisterStartupScript(this.GetType(), "Mensaje", "alert('Maximo Puede Enviar 10MB En Archivos');", true);
    }

    else
    {
        //Moving on to send email
    }
}

我想要的是,如果blnBytes等于false(当所有组合文件都超过10mb时它会变为false),它只会显示一条错误消息,但即使单个文件大于10mb,我怎么能让它工作? ....

希望你能帮助我 感谢

2 个答案:

答案 0 :(得分:2)

您必须在web.config中启用大文件支持(httpRuntime,maxRequestLength参数)。

以下是样本:

    <httpRuntime requestValidationMode="2.0"
     executionTimeout="6000"
     maxRequestLength="20485760"
     requestLengthDiskThreshold="80"
     useFullyQualifiedRedirectUrl="false"
     minFreeThreads="8"
     minLocalRequestFreeThreads="4"
     appRequestQueueLimit="5000"
     enableKernelOutputCache="true"
     enableVersionHeader="true"
     requireRootedSaveAsPath="true"
     enable="true"
     shutdownTimeout="90"
     delayNotificationTimeout="5"
     waitChangeNotification="0"
     maxWaitChangeNotification="0"
     enableHeaderChecking="true"
     sendCacheControlHeader="true"
     apartmentThreading="false" />

有关此内容的更多信息,请访问: http://msdn.microsoft.com/en-us/library/e1f13641(v=vs.71).aspx

答案 1 :(得分:2)

你可以试试这个..

foreach (HttpPostedFile hpf in hfc)
{
    if(hpf.ContentLength > 11000000)
    {
        blnBytes = false;
        break;
    }
    intBytes += hpf.ContentLength;
    if (intBytes > 11000000)
    {
        blnBytes = false;
        break;
    }
}