首页 > 解决方案 > 如何在 asp.net core(3.1) 应用程序中设置 maxJsonLength

问题描述

如果文件大小在 100mb 以内,则 asp.net 核心 webAPI 控制器返回文件的 base64 字符串,应用程序将下载该文件。但是当文件大小超过接近 100mb AJAX 请求时会出现 500 错误“System.ArgumentException:长度为 216539224 的 JSON 值太大且不受支持。”我在 ASP.NET 核心项目中添加了 web.config 文件并设置 maxJsonLength = web.config 文件中的 2147483644 但问题仍未解决。需要帮助

<configuration> 
   <system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="2147483644"/>
           </webServices>
       </scripting>
   </system.web.extensions>
</configuration>

[HttpPost]
        public async Task<ActionResult> CreatePDF(string json) {
            try
            {
                string userFilePath = @"D:\file.pdf";
                string base64File = Convert.ToBase64String(System.IO.File.ReadAllBytes(userFilePath));
                ErrorLog.LogInformation("Task Compeleted " + userFilePath);
                return Json(new { ReturnCode = 200, FileName = "test", ReturnMessage = "File Downloaded Successfully!", bse64 = base64File });

            }
            catch (Exception ex)
            {

                throw;
            }
        }

标签: asp.net-coreasp.net-core-3.1

解决方案


推荐阅读