I was able to upload upto 4 GB using Web APIs and IIS by making below changes.
In web api project, below 2 changes in web.config to set max lengths.
<requestFiltering>
<requestLimits maxAllowedContentLength="4294967295"/>
</requestFiltering>
<httpRuntime targetFramework="4.5.2" executionTimeout="2400" maxRequestLength="2147483647"/>
Adding the chunked header at client side while calling web api as shown below which stops IIS from restricting files above 2 GB by streaming the file-
HttpClient.DefaultRequestHeaders.Add("Transfer-Encoding", "chunked");
Adding below code before reading the stream at server side (web api controller), to use overloaded method GetBufferlessInputStream(disableMaxLength) to ignore max request length of 2 GB-
var content = new StreamContent(HttpContext.Current.Request.GetBufferlessInputStream(true));
foreach(var header in Request.Content.Headers) {
content.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
await content.ReadAsMultipartAsync(streamProvider);
Change the policy selector in server side, so that buffering is disabled & file is streamed instead.Add below class to override WebHostBufferPolicySelector for your controller (e.g. "File" controller in snippet below)-
public class NoBufferPolicySelector: WebHostBufferPolicySelector {
public override bool UseBufferedInputStream(object hostContext) {
var context = hostContext as HttpContextBase;
if (context != null && context.Request.RequestContext.RouteData.Values["controller"] != null) {
if (string.Equals(context.Request.RequestContext.RouteData.Values["controller"].ToString(), "File", StringComparison.InvariantCultureIgnoreCase))
return false;
}
return true;
}
public override bool UseBufferedOutputStream(HttpResponseMessage response) {
return base.UseBufferedOutputStream(response);
}
}
Add below into Register method-
GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), new NoBufferPolicySelector());
Hope this helps anyone out there.