There are a couple of things to consider when dealing with large file uploads and ASP.NET Web API. First of all, there are typical issues related to hosting within IIS context, and the limitations imposed on the service by default by the ASP.NET framework.Secondly, there is the usual problem of having to deal with out of memory issues when allowing external parties or users to upload buffered large files. As a consequence, streamed uploads or downloads can greatly improve the scalability of the solutions by eliminating the need for large memory overheads on buffers.Let’s have a look at how to tackle these issues.1. Configuring IISIf you are using Web Hosting within IIS context, you have to deal with maxAllowedContentLength and/or maxRequestLength – depending on the IIS version you are using.Unless explicitly modified in web.config, the default maximum IIS 7 upload filesize is 30 000 000 bytes (28.6MB). If you try to upload a larger file, the response will be 404.13 error.If you use IIS 7+, you can modify the setting, in the example below to 2GB. It’s worth remembering, that out of the two configuration elements, whichever request length limit is smaller, will take precedence. Out of memory through bufferingWeb API, by default buffers the entire request input stream in memory.
Since we just increased the file upload limit to 2GB, you can easily imagine how this could cause OOM really quickly – i.e. Through a couple of simultaneous uploads going on.Fortunately there is an easy way to force Web API into streamed mode of dealing with the uploaded files, rather than buffering the entire request input stream in memory.We were given control over this through (yes, it’s part of Web API RTM) and Kiran Challa from MSFT had a very about how to use that.
Basically, we need to replace the default IHostBufferPolicySelector with our custom implementation. Replace ( typeof ( IHostBufferPolicySelector ), new NoBufferPolicySelector ( ) );I will not go into details about how to upload files to Web API – this was already covered in. Instead, here is a simple example of how resource management on your server improves when we switch off input stream buffering. Below is memory consumption when uploading a 400MB file on my local machine, with default Web API settings:and here, after replacing the default IHostBufferPolicySelector:So what happens under the hood? Well, in the Web API pipeline HttpControllerHandler makes the decision whether to use buffering of the request input stream or not by inspecting the lazily loaded instance of policy selector (a.k.a.
Our IHostBufferPolicySelector). If the request should not be buffered (like ours), it uses the good old HttpRequest.GetBufferlessInputStream from the System.Web namespace. As a result the underlying Stream object is returned immediately and we can begin processing the request body before its complete contents have been received.Unfortunately, while it seems very straightforward, it’s not all rainbows and unicrons. As you have probably already noticed, all this, is very heavily relying on System.Web making it unusable outside of the hosting context of ASP.NET. As a result, the beautiful symmetry of Web API in self- and web-host scenarios is broken here.
Self hostIf you are trying to achieve any of the above in self hosting, you are forced to use a workaround – and that is setting your self host’s configuration to. TransferMode = TransferMode. Archives. (1).
(1). (1).
(1). (1). (2). (1). (1). (2).
Download File Asp Net
(1). (2). (3). (1). (2). (2). (1).
(1). (2). (2). (1). (1). (2). (1).
(1). (3).
(1). (1). (1). (2).
(2). (2). (1). (4). (1).
(2). (4). (3). (3). (3). (2).
(1). (2). (2). (2). (3).
(1). (2). (1). (1).
(2). (1). (1). (2). (2).
(2). (3). (2). (2).
(2). (3). (1). (2).
Font Files
(2). (3).
(1). (3). (2).
Ing Text Files In Asp.net Outlook
(2). (2).
(3). (3). (3).
(3). (4). (5).
(4). (7).
(3). (5). (5). (5). (6). (5). (6).
(6). (5). (6). (1).