【问题标题】:byte[] over 3MB comes up NULL in API C#超过 3MB 的字节 [] 在 API C# 中出现 NULL
【发布时间】:2021-11-09 17:17:49
【问题描述】:

我有一个用 .NET 4.7.2 编写的桌面应用程序,它使用 API 将文件上传到 SharePoint。

这是对 API 的调用

var responseMessage = this
                      .Client
                      .PostAsync("/Upload", new StringContent(JsonConvert.SerializeObject(file), Encoding.UTF8, "application/json"))
                      .Result;

if (!responseMessage.IsSuccessStatusCode)
    throw new Exception("Save to Sharepoint Failed");

return responseMessage;

file对象中的内容类型和byte[]都不错,成功上传小于3MB的文件就好了。

API .NET 4.7.1
一旦它到达 request.doc 对象,如果超过 ~3MB,则 request.doc 将为 NULL,因此上传将失败。

[HttpPost]
[Route("Upload")]
public async Task<IHttpActionResult> SaveFileToSharePointStream([FromBody] SharepointFileUploadRequest request)
{
    using (var ms = new MemoryStream(request.doc))
    {
        var result = await _sharepointBll.SaveFileToSharePointStream(request.newFileName, ms, request.folderPath, request.fileTags);
        return Ok(result);
    }
}

我尝试在客户端和服务器上都添加 web.config 无济于事。

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits>
        <headerLimits>
          <add header="Content-type" sizeLimit="30000000" />
        </headerLimits>
      </requestLimits>
    </requestFiltering>
  </security>
</system.webServer>

我认为问题可能出在String.Content(),但我似乎找不到增加可以传递给它的数据量的方法。

【问题讨论】:

  • PostAsync() 需要时间(因此是异步的),您必须等到帖子完成后 IsSuccessStatusCode 才能提供可靠的值。
  • 您使用的是 Microsoft Graph API 吗?如果是这样,请注意有两种方法,one for small files, up to 4MBone for any file size。也许你正在使用第一个,所以你达到了 4MB 的限制
  • 我没有使用 Graph API
  • 只是想知道,您希望将 Content-Type 标头的值长度限制为 30 MB 会达到什么目的?
  • 我认为限制太低了,我需要定义更高。我试图提高天花板,而不是降低极限。

标签: c# .net api file


【解决方案1】:

您可以尝试将SharepointFileUploadRequest 作为给定代码中的参数传递,而不是传递new StringContent(JsonConvert.SerializeObject(file)

【讨论】:

    【解决方案2】:

    在配置中的 api 上,我必须添加 maxRequestLength

    <httpRuntime targetFramework="4.7.1" executionTimeout="1000" maxRequestLength="30720" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 2011-04-14
      • 2017-04-30
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多