我是一个菜鸟,我只是在努力,2021正视自己,面对2021!

微信小程序上传文件(报错处理方式)

1.未显示页面,因为请求实体过大

  a .问题描述

    在IIS上部署一个可以接受文件上传的netCore WebApi,使用`Http`验证时没有任何问题,使用`SSL`后;通过微信小程序Post文件流,就会导致异常:`未显示页面 因为请求实体过大`的413 错误;但是在 Chrome 内核的微信小程序编辑工具中,则不存在该问题。
  b.问题原因
    客户端发起一个请求后,IIS会收到足以解析请求标头的数据,但不会收到整个请求实体正文,如果发现需要客户端证书时,将尝试重新协商连接;但此时客户端正等待向IIS发送请求中的其余数据。因此,如果让客户端能接受重新协商,则必须使用SSL预加载功能预加载请求实体正文,此时则可能引起默认设置值`UploadReadAheadSize`长度太小的问题。
  c. 解决方案

    c.1 第一步:进入 `cd C:\Windows\System32\Inetsrv` 目录执行命令行

      appcmd.exe list config -section:system.webServer/serverRuntime // 查看当前设置的 UploadReadAheadSize 大小(byte)

      appcmd.exe set config -section:system.webServer/serverruntime /uploadreadaheadsize:20480000 // 这个要设置够大

    c.2 第二步 还需要在web.config文件中更改上传文件的大小

      在创建的项目里面是没有 “web.config” 文件的。

      上传大文件时需要配置下文件的大小,需要在 “config” 文件里配置。创建一个或复制一个 “web.config”

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <!--单位:字节。 -->
        <requestLimits maxAllowedContentLength="1073741824" />
        <!-- 1 GB -->
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

2. Web请求报出"超过了最大请求长度[注意:重启IIS]"

  a. 错误原因:asp.net默认最大上传文件大小为4M,运行超时时间为90S。
 
  b.解决方案:修改web.config文件可以改变这个默认值     
<configuration>   
   <system.web>  
         <httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
   </system.web>  
<configuration> 

3. 最终WebConfig的配置如下

<?xml version="1.0" encoding="utf-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  https://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <!--Mysql数据库连接字符串-->
  <connectionStrings>
    <!--<add name="PNF_DB_Snapshot" connectionString="server=115.28.103.116;userid=chuxincms;pwd=chuxinmhanyinglong19900810_prod;port=3306;database=pnf_snapshot;sslmode=none;charset=utf8mb4;" />-->
    <add name="PNF_DB_Snapshot" connectionString="server=127.0.0.1;userid=root;pwd=snaphot!@#2019;port=3405;database=pnf_snapshot;sslmode=none;charset=utf8mb4;" />
  </connectionStrings>
  <appSettings>
    <add key="Wechat_AppId" value="wx147013d69affee24" />
    <add key="Wechat_Secret" value="e834dff786285d846fde8f71e76a3850" />
    <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.web>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2" maxRequestLength="1048576" executionTimeout="3600" />
    <customErrors mode="Off" />
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Configuration.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.0.2.0" newVersion="1.0.2.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, 
Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" /> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider,
Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
" warningLevel="4"
compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> </compilers> </system.codedom> <system.webServer> <httpRedirect enabled="false" destination="" /> <rewrite> <rules> <rule name="https" enabled="true" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAny"> <add input="{HTTPS}" pattern="^OFF$" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" /> </rule> </rules> </rewrite> <staticContent> <mimeMap fileExtension=".woff2" mimeType="application/x-font-woff" /> </staticContent> <security> <requestFiltering> <!--单位:字节。 --> <requestLimits maxAllowedContentLength="1073741824" /> <!-- 1 GB --> </requestFiltering> </security> </system.webServer> </configuration> <!--ProjectGuid: CA844D14-20AE-45E5-B5D1-2502A0D14389-->
posted @ 2020-12-25 09:41  Kencery  阅读(1227)  评论(0编辑  收藏  举报
友情链接:初心商城