0

我有以下代码段,它适用于小文件。但是,如果文件较大,则应用程序正在加载很长时间并收到请求的资源上不存在“Access-Control-Allow-Origin”标头。

[HttpPost]
[ScopeAuthorize(Constants.ClaimScopeSGCanManageAll, Constants.ClaimScopeUserCanManage)]
[DisableRequestSizeLimit, RequestFormLimits(MultipartBodyLengthLimit = int.MaxValue, ValueLengthLimit = int.MaxValue)]
public async Task<IActionResult> UploadFile()
{

    if (!Request.Form.Files.Any())
    {
        throw new Common.Exceptions.ValidationException("Empty file");
    }

    IFormFile formFile = Request.Form.Files[0];

    var csvDatas = new List<PatientsCSVItem>();
    using (var reader = new StreamReader(formFile.OpenReadStream()))
    {
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var values = line.Split(';');

          //process csv rows
        }

    }

    PatientCsvLog executionLog = _patientCsvManager.AddOrUpdatePatientsByCsvData(csvDatas, _userManager.GetLoggedUserId(User));

    if (executionLog == null)
    {
        throw new ArgumentNullException(nameof(executionLog));
    }

    var response = new
    {
        NumberRecordImported = executionLog.NumberRecordImported,
        NumberRecordUpdated = executionLog.NumberRecordUpdated,
        NumberRecordDiscarded = executionLog.NumberRecordDiscarded,
        DiscardedRecordList = executionLog.DiscardedRecordList
    };

    return Ok(response);

}
4

1 回答 1

0

如果你在IIS下运行尝试设置maxAllowedContentLength参数web.config
默认值约为 28.6MB。

<?xml version="1.0" encoding="utf-8"?>
<configuration> 
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="7147483648" />
      </requestFiltering>
    </security> 
  </system.webServer>
</configuration>
于 2022-02-19T10:18:01.960 回答