【问题标题】:How to validate size of files while uploading on ASP.NET C#?在 ASP.NET C# 上上传时如何验证文件的大小?
【发布时间】:2012-09-11 04:43:32
【问题描述】:

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

我以此作为参考来创建它: 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,我怎样才能使它工作? ....

希望你能帮助我 谢谢

【问题讨论】:

    标签: c# jquery asp.net validation upload


    【解决方案1】:

    您必须在 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

    【讨论】:

      【解决方案2】:

      你可以试试这个..

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-12
        • 2012-04-12
        相关资源
        最近更新 更多