Where to use GetBufferlessInputStream?

2019-08-02 17:16发布

问题:

Where do I set HttpContext.Request.GetBufferlessInputStream(true)? I am trying to allow the user to upload files larger than 2GB and obviously I am running into the "maxRequestLength" int type restriction. I have tried to create a StreamReader the following way:

var reader = new StreamReader(HttpContext.Request.GetBufferlessInputStream(true));

But I'm doing it in a controller and I end up getting the following error:

"This method or property is not supported after HttpRequest.Form, Files, InputStream, or BinaryRead has been invoked."

So I'm guessing I have to make this change before the controller method gets called. I've searched stack overflow and many other websites for answers, but all I've found is how to use it not where to use it.

Thank you for your time and helping me out with this.

回答1:

You are going to have to implement your logic in an HttpModule - see this similar question: How should I use HttpRequest.GetBufferlessInputStream?

Update - actually you are better off writing your own HttpHandler instead of a Module

Using GetBufferlessInputStream is basically telling ASP.NET that you are going to handle the entire request, instead of only a portion of it. As you've seen when you use this in a module, after your module completes, the request continues through the remainder of the request pipeline, and ASP.NET is going to expect to be able to read part of the input.

By writing your own HttpHandler, you are responsible for the entirety of the processing - so this would only be for handling the large upload.

I found a link with a sample that looks pretty relevant for you https://blogs.visoftinc.com/2013/03/26/streaming-large-files-asynchronously-using-net-4-5/