I have a Web API which uploads the File content to the server.
[HttpPost]
[Route("SaveFileContent")]
public async Task<FileResponse> SaveFileContent([FromForm] SaveFileContentRequest request)
{
return await _service.SaveFile(request);
}
This is my call to the API:
public async Task<FileResponse> SaveFileContent(SaveFileContentRequest request)
{
try
{
var uri = "https://www.mycompanyurl.com";
using (var client = new HttpClient())
{
using (var form = new MultipartFormDataContent())
{
using (var fileContent = new ByteArrayContent(request.File))
{
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
form.Add(fileContent, "file", request.FileName);
form.Add(new StringContent(request.MemberId), "MemberId");
form.Add(new StringContent(request.Country), "Country");
client.BaseAddress = new Uri(uri);
HttpResponseMessage response = await client.PostAsync("/api/Document/SaveFileContent", form);
FileResponse result = JsonConvert.DeserializeObject<FileResponse>(response.Content.ReadAsStringAsync().Result);
return result;
}
}
}
}
catch (Exception ex)
{
ex.LogError(ex);
}
}
With this, the call is successfully made to the API and the file is saved on the server.
But, when I added 2 new StringContent to the MultipartFormDataContent:
form.Add(new StringContent(request.Source), "Source");
form.Add(new StringContent(request.UserName), "UserName");
I am getting this exception:
Bytes to be written to the stream exceed the Content-Length bytes size specified
How do I resolve this?