You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular与.NET Core大模型读取报错:大文件上传502失败求助

大文件上传超时/502错误解决方案

1. 调整.NET Core后端的请求大小与超时限制

在Program.cs(.NET 6+)或Startup.cs(旧版本)中配置请求参数,同时配合配置文件调整:

// 配置表单请求限制
builder.Services.Configure<FormOptions>(options =>
{
    options.MultipartBodyLengthLimit = 1073741824; // 设为1GB,可按需调整
    options.ValueLengthLimit = int.MaxValue;
});

// 配置Kestrel服务器的请求限制
builder.WebHost.ConfigureKestrel(serverOptions =>
{
    serverOptions.Limits.MaxRequestBodySize = 1073741824;
    serverOptions.Limits.RequestTimeout = TimeSpan.FromMinutes(10); // 延长超时到10分钟
});

在appsettings.json中补充配置:

{
  "Kestrel": {
    "Limits": {
      "MaxRequestBodySize": 1073741824,
      "RequestTimeout": "00:10:00"
    }
  }
}

2. 配置反向代理(IIS/Nginx)的限制

502错误大多来自反向代理的超时或大小限制,需对应调整:

  • IIS:修改web.config添加以下节点:
<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="1073741824" /> <!-- 1GB -->
    </requestFiltering>
  </security>
  <aspNetCore requestTimeout="00:10:00" />
</system.webServer>
  • Nginx:修改站点配置文件:
server {
    client_max_body_size 1G;
    proxy_connect_timeout 600s;
    proxy_send_timeout 600s;
    proxy_read_timeout 600s;
}

3. 前端延长请求超时时间

在调用uploadModelsonRevitandForge函数时,调整请求的超时参数:

  • 若用Axios:
axios.post('/api/upload', formData, {
    timeout: 600000, // 10分钟(毫秒单位)
    maxContentLength: 1073741824,
    maxBodyLength: 1073741824
});
  • 若用原生Fetch:
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 600000);

fetch('/api/upload', {
    method: 'POST',
    body: formData,
    signal: controller.signal
})
.then(res => {
    clearTimeout(timeoutId);
    // 处理响应逻辑
});

4. 实现分块上传(大文件场景推荐)

单文件上传超过1GB时稳定性差,建议拆分文件分块上传:

  • 前端将文件分割为固定大小的块(比如10MB/块),上传时携带块编号、总块数、文件唯一ID
  • 后端接收后临时存储块,所有块上传完成后合并为完整文件
    后端示例代码:
[HttpPost("upload-chunk")]
public async Task<IActionResult> UploadChunk([FromForm] IFormFile chunk, [FromForm] int chunkIndex, [FromForm] int totalChunks, [FromForm] string fileId)
{
    var tempDir = Path.Combine(Path.GetTempPath(), fileId);
    Directory.CreateDirectory(tempDir);
    var chunkPath = Path.Combine(tempDir, chunkIndex.ToString());
    
    using (var stream = new FileStream(chunkPath, FileMode.Create))
    {
        await chunk.CopyToAsync(stream);
    }
    
    // 检查是否所有块上传完成
    var uploadedCount = Directory.GetFiles(tempDir).Length;
    if (uploadedCount == totalChunks)
    {
        var finalPath = Path.Combine("uploads", $"{fileId}.rvt");
        using (var finalStream = new FileStream(finalPath, FileMode.Create))
        {
            for (int i = 0; i < totalChunks; i++)
            {
                var chunkFilePath = Path.Combine(tempDir, i.ToString());
                using (var chunkStream = new FileStream(chunkFilePath, FileMode.Open))
                {
                    await chunkStream.CopyToAsync(finalStream);
                }
                File.Delete(chunkFilePath);
            }
        }
        Directory.Delete(tempDir);
        return Ok(new { msg = "文件上传完成" });
    }
    
    return Ok(new { msg = "块上传成功", uploadedCount });
}

5. 检查服务器资源

  • 确保服务器磁盘有足够空间存储大文件
  • 大文件上传会占用较多内存,可增加服务器内存或调整.NET垃圾回收配置

内容的提问来源于stack exchange,提问作者Sukanya Gavhane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 11:46:29