asp.net - HTTP 错误 400 - 使用 AJAX 上传大文件时请求错误

标签 asp.net http asyncfileupload

我收到来自 ASP.Net 网站的关于使用 AJAX 提交大文件 2GB 的错误请求。

我的 HTTP 运行时设置 <httpRuntime maxRequestLength="2147483647" executionTimeout="3600"/>

请帮忙。

网络配置
`

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="microsoft.web.services3" type="Microsoft.Web.Services3.Configuration.WebServicesConfiguration, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <connectionStrings>
    <remove name="LocalSqlServer"/>
    <add name="LocalSqlServer" connectionString="" providerName="System.Data.SqlClient"/>
    <add name="psDevDataConnectionString1" connectionString="" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="50000000" />
      </webServices>
    </scripting>
  </system.web.extensions>
  <system.web>
    <authentication mode="Forms"/>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime maxRequestLength="2147483647" executionTimeout="3600"/>
    <membership defaultProvider="xplProvider">
      <providers>
        <add name="xplProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="/" requiresUniqueEmail="true" passwordFormat="Hashed" maxInvalidPasswordAttempts="10" minRequiredPasswordLength="4" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
      </providers>
    </membership>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
    <customErrors mode="RemoteOnly" defaultRedirect="~/sysError.aspx">
      <error statusCode="404" redirect="~/infoCollector.aspx"/>
    </customErrors>
    <pages controlRenderingCompatibilityVersion="4.0" clientIDMode="Static"/>
  </system.web>
  <system.webServer>
    <handlers>
      <add name="DocImage" verb="GET,POST" path="DocImage.axd" type="DotnetDaddy.DocumentViewer.DocImageHandler, DocumentViewer"/>
      <add name="pngs" verb="*" path="ClientPortals/images/*" type="Project.UserInterface.ClientPortals.image" preCondition="managedHandler"/>
    </handlers>
    <modules>
      <add name="UploadModule" type="RTE.UploadModule,RichTextEditor"/>
      <add name="CuteEditor.UploadModule" type="CuteEditor.UploadModule,CuteEditor"/>
    </modules>
    <httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
      <remove statusCode="404"/>
      <error statusCode="404" path="/scenter.aspx" responseMode="ExecuteURL"/>
    </httpErrors>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="4294967296"/>
      </requestFiltering>
    </security>
  </system.webServer>
  <appSettings>
    <add key="PageInspector:ServerCodeMappingSupport" value="Disabled"/>
  </appSettings>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="ServiceSoap">
          <security mode="Transport"/>
        </binding>
        <binding name="ServiceSoap1"/>
        <binding name="sms2SOAPbasicHttpBinding"/>
        <binding name="sms2SOAPbasicHttpsBinding">
          <security mode="Transport"/>
        </binding>
      </basicHttpBinding>
      <wsHttpBinding>
        <binding name="sms2wsHttpBinding">
          <security mode="None"/>
        </binding>
        <binding name="sms2wsHttpBindingSecure">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://api.authorize.net/soap/v1/Service.asmx" binding="basicHttpBinding" bindingConfiguration="ServiceSoap" contract="ArbApiSoap.ServiceSoap" name="ServiceSoap"/>
      <endpoint address="http://sms2.cdyne.com/sms.svc/Soap" binding="basicHttpBinding" bindingConfiguration="sms2SOAPbasicHttpBinding" contract="cdyneSMS.Isms" name="sms2SOAPbasicHttpBinding"/>
      <endpoint address="https://sms2.cdyne.com/sms.svc/SecureSoap" binding="basicHttpBinding" bindingConfiguration="sms2SOAPbasicHttpsBinding" contract="cdyneSMS.Isms" name="sms2SOAPbasicHttpsBinding"/>
      <endpoint address="http://sms2.cdyne.com/sms.svc/WS" binding="wsHttpBinding" bindingConfiguration="sms2wsHttpBinding" contract="cdyneSMS.Isms" name="sms2wsHttpBinding"/>
      <endpoint address="https://sms2.cdyne.com/sms.svc/WS" binding="wsHttpBinding" bindingConfiguration="sms2wsHttpBindingSecure" contract="cdyneSMS.Isms" name="sms2wsHttpBindingSecure"/>
    </client>
  </system.serviceModel>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="AWSSDK" publicKeyToken="9f476d3089b52be3" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-2.3.55.0" newVersion="2.3.55.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <microsoft.web.services3>
    <messaging>
      <executionTimeoutInSeconds value="300" />
      <maxMessageLength value="524288000" />
    </messaging>
  </microsoft.web.services3>
</configuration>

`

最佳答案

maxRequestLength 的单位是千字节,而不是字节,所以 2147483647 千字节大约是 2000 GB,这已经很多了。 2GB = 2097152 KB。试试改成2097152

另一方面,maxAllowedContentLength 以字节为单位。

建议将这两个值都放在 web.config 中。

The maxRequestLength indicates the maximum file upload size supported by ASP.NET, the maxAllowedContentLength specifies the maximum length of content in a request supported by IIS. Hence, we need to set both maxRequestLength and maxAllowedContentLength values to upload large files.

<system.web>
    <httpRuntime maxRequestLength="2097152" executionTimeout="3600"/>
</system.web>
<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="2147483648" />
        </requestFiltering>
    </security>
</system.webServer>

关于asp.net - HTTP 错误 400 - 使用 AJAX 上传大文件时请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37420420/

相关文章:

c# - 如何识别字符串是否包含 unicode 字符?

asp.net - 从代码隐藏文件创建 JSON 数组

c# - 服务栈拦截所有Http To Service

http - 响应分块时获取整个响应正文?

c# - AsyncFileUpload 回发导致双重上传

javascript - 旧的(以前的 html5)ajax 文件上传框架如何工作?

c# - 如何在不禁用验证的情况下将 HTML 或其他类似标记安全地传递到文本框中?

c# - 如何加载ascx文件中的资源 key ?

http - HTTP Content-Length 与实际 body size 不一致怎么办?

asp.net-mvc-4 - 如果超过特定大小限制,如何阻止文件上传?