我正在尝试使用 FileUpload 控件在我的asp网站上传文件,但是,如果文件大小超过 maxRequestLength ,如 Web中所示我网站的.config ,页面超时。为了防止这种情况,我试图将以下代码插入Global:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.ContentLength > MAX_FILE_SIZE_BYTES)
{
Response.Redirect("~/ErrorMaxFileSizeExceeded.aspx");
}
}
问题是,当超过maxRequestLength时,从未达到上述代码,但为什么呢?浏览器指示它开始上传文件,并且用户可以看到进度,但进度停留在 0%。
有许多建议增加 maxRequestLength 以适应用户上传,但是,这不是一个好的解决方法。我的一些用户可能会(本地)尝试上传可能达到1GB的大量文件。我需要在没有页面超时的情况下提供适当的错误消息。
附加:Web.config
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-KnoLex-20140916165248;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-KnoLex-20140916165248.mdf" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<!-- change here for max file upload size-->
<httpRuntime targetFramework="4.5" executionTimeout="240" maxRequestLength="20480000" enable ="true"/>
<pages>
<namespaces>
<add namespace="System.Web.Optimization" />
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
</controls>
</pages>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="~/" />
</authentication>
<profile defaultProvider="DefaultProfileProvider">
<providers>
<add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</profile>
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<roleManager defaultProvider="DefaultRoleProvider">
<providers>
<add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</roleManager>
<!--
If you are deploying to a cloud environment that has multiple web server instances,
you should change session state mode from "InProc" to "Custom". In addition,
change the connection string named "DefaultConnection" to connect to an instance
of SQL Server (including SQL Azure and SQL Compact) instead of to SQL Server Express.
-->
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
</providers>
</sessionState>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
答案 0 :(得分:0)
最好在客户端使用验证功能(即使用javascript)
<script type="text/javascript" >
function Validate() {
var msg = "";
var bIsValid = true;
var uploadedFile = document.getElementById("<%=fileUpload.ClientID %>");
if (uploadedFile.files[0].size > 20480000) // greater than size
{
msg += "File Size is limited to 20480000 !";
alert(msg);
bIsValid = false;
return false;
}
//On Success
return true;
}
</script>
在按钮Validate()
事件中调用此函数OnClientClick
,以防止用户上传文件超过您定义的尺寸。