ASP.NET FileUpload无法接收10MB以上文件的问题求助
关于ASP.NET FileUpload控件大文件上传失效的问题及临时解决方案
原始问题
使用asp:FileUpload控件实现文件上传功能,已在web.config中配置允许最大50MB文件上传,但选择10MB以上的文件时,控件无法识别该文件(显示未选择状态),10MB以下的文件可正常显示文件名。后端C#上传脚本尚未触发,问题出在前端控件识别阶段。
web.config配置内容:
<?xml version="1.0" encoding="utf-8"?> <!-- For more information on how to configure your ASP.NET application, please visit https://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <!-- Traditional API: Register a custom SessionIDManager for BotDetect requests --> <compilation targetFramework="4.7.2" debug="false" /> <httpRuntime targetFramework="4.7.2" maxRequestLength="51200" requestLengthDiskThreshold="51200" executionTimeout="3600"/> </system.web> <appSettings> <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> <system.codedom> <compilers> <compiler extension=".cs" language="c#;cs;csharp" warningLevel="4" compilerOptions="/langversion:7.3 /nowarn:1659;1699;1701;612;618" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> <compiler extension=".vb" language="vb;vbs;visualbasic;vbscript" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\"Web\" /optionInfer+" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </compilers> </system.codedom> <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="52428800" /><!--50MB--> </requestFiltering> </security> <validation validateIntegratedModeConfiguration="false" /> </system.webServer> </configuration>
原FileUpload控件代码:
<div class="u-active-custom-color-4 u-align-center-sm u-align-center-xs u-border-2 u-border-custom-color-4 u-btn u-btn-round u-button-style u-custom-color-6 u-custom-font u-heading-font u-hover-custom-color-5 u-radius-30"> <asp:FileUpload ID="FileUploadPpt" runat="server" /> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUploadPpt" ValidationExpression="(.+\.([Pp][Pp][Tt][Xx])|.+\.([Pp][Pp][Tt]))"></asp:RegularExpressionValidator> <asp:Label ID="LabelPptFile" runat="server" Text=""></asp:Label> </div>
用户点击页面末尾的“Create”按钮才会执行后端上传脚本,但目前根本到不了这一步。
临时解决方案
尝试多种常规方法无效后,找到一个不太合理的折中方案:添加一个额外的FileUpload控件,放在原控件之前,该新控件可正常接收大文件。但删除原控件只保留新控件无效,最终只能保留两个控件并隐藏第一个控件(通过拆分div块实现,不能直接设置控件的Visible属性,否则会引发新问题)。
修改后的代码如下:
<asp:FileUpload ID="FileUpload2" runat="server" /> </div> <div class="u-active-custom-color-2 u-border-2 u-border-white u-btn u-btn-round u-button-style u-custom-color-3 u-custom-font u-heading-font u-hover-custom-color-1 u-radius-30"> <asp:FileUpload ID="FileUploadPpt" runat="server" /> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUploadPpt" ValidationExpression="(.+\.([Pp][Pp][Tt][Xx])|.+\.([Pp][Pp][Tt]))"></asp:RegularExpressionValidator> <asp:Label ID="LabelPptFile" runat="server" Text=""></asp:Label> </div>
此方案虽不优雅,但能解决大文件无法被控件识别的问题,分享给遇到相同问题的开发者参考。
内容的提问来源于stack exchange,提问作者starspejd
相关产品推荐
相关产品推荐

