【问题标题】:WEB API - large byte array issueWEB API - 大字节数组问题
【发布时间】:2021-03-03 16:03:53
【问题描述】:

我正在尝试通过我的 WEB API 上传大型 CSV 文件。现在这已经正常工作,直到我上传大小为 350 MB 的文件。现在我有一个大小为 400 MB 的文件,它没有通过我的 API 上传。 看起来我发布的字节数组正在变为空。

我将这个字节数组发布到 API 的 C# 代码 - Buff 是一个字节数组

 HttpClient client = new HttpClient();
  client.BaseAddress = new Uri(apiurl);
  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
 HttpResponseMessage response = client.PostAsJsonAsync(APIUrl, buff).Result;

而被调用的API方法是-

 [HttpPost]
    [Route("api/UploadDocument/{Organisation}/{OriginalFileName}/{FlexFd}/{RecordRefEntityName}/{RecordRefId}")]
    public IHttpActionResult UploadDocument([FromBody] byte[] File_Stream, string Organisation, string OriginalFileName, string FlexField1, string RecordRefEntityName, string RecordRefId,  [FromUri] string FolderName)
    {
        EDocumentRepositry uploadedDocument = new EDocumentRepositry();
        try
        {
            uploadedDocument = doc.UploadFile(File_Stream, Organisation, OriginalFileName, FlexField1, RecordRefEntityName, RecordRefId, FolderName);
            return Ok(uploadedDocument);
        }
        catch (Exception ex)
        {

        }
    }

当我发布大字节时,此处 File_stream 显示为 null(array.length 显示值为 407353839)

现在我有以下 API 配置设置

 <httpRuntime targetFramework="4.5.2" executionTimeout="100000" maxRequestLength="214748364" />

  <requestFiltering>
    <requestLimits maxAllowedContentLength="1073741824" />
  </requestFiltering>

【问题讨论】:

  • 您的 maxRequestLength 也需要足够大。
  • @Mufaka 尝试设置 maxRequestLength="2097152" 和 maxAllowedContentLength="2147483648" 但仍然遇到同样的错误
  • 还尝试了 maxRequestLength="2147483647" 和 maxAllowedContentLength="4294967295"。仍然出现错误。不确定这些配置更改是否得到反映..
  • 我认为您可以使用HttpRequest.GetBufferlessInputStream 以便它可以读取无限的流长度。您已在 IIS 上设置了有关文件限制的所有配置。所以可能会受到代码的影响。

标签: c# iis asp.net-web-api httpclient webapi


【解决方案1】:

只需添加此 [HttpPost, DisableRequestSizeLimit] 并更改为在客户端使用表单数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 2015-08-24
    相关资源
    最近更新 更多