Home > database >  byte[] over 3MB comes up NULL in API C#
byte[] over 3MB comes up NULL in API C#

Time:11-12

I have an desktop application written in .NET 4.7.2 that uses an API to upload files to SharePoint.

This is the call to the 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;

The content type and the byte[] are all good in the file object, it successfully uploads a file less than 3MB just fine.

The API .NET 4.7.1
Once it gets here the request.doc object, if over ~3MB then the request.doc will be NULL and as such the upload will fail.

[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);
    }
}

I tried adding in the web.config on both the client and the server to no avail.

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

I think the issue might be with String.Content(), but I can't seem to find a way to increase the amount of data I can pass to it.

CodePudding user response:

Instead of passing new StringContent(JsonConvert.SerializeObject(file), you can try by passing the SharepointFileUploadRequest as parameter from your given code.

CodePudding user response:

On the api in the config I had to add maxRequestLength

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