微信小程序上傳文件(報錯處理方式)


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-->


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM