2

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 Answer 1

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/

Sign up to request clarification or add additional context in comments.

12 Comments

Cool. Thanks for the info. I created an HttpModule, but now I have a different issue I outline here: stackoverflow.com/questions/48776028/… Think you could help me out with that?
That worked really well. Thanks. But now I have a different problem. How would I extract the file I post so I can save it. I don't want to write it to a text file so StreamWriter isn't what I want to use. Before this I was just appending the file to formdata in Jquery like the first answer to this question: link and then I was accepting it as a HttpPostedFileBase in my controller.
I've read here link how 'BinaryRead()', 'Form', 'Files', and 'InputStream' won't cooperate with 'GetBufferlessInputStream()' so I'm kind of lost how to extract the file from the Request so I can save it. Thanks!
You will get a Stream object from GetBufferlessInputStream - that stream represents the uploaded file. Depending upon the content type of the file, you will need to decide how to write it. If it's just a large text file, you should be able to create a StreamWriter and pass it the Stream. If it's a large binary file, you should create a BinaryWriter and pass it the Stream.
So the file is a .db file so I would use a StreamWriter but doing something similar to this doesn't work because 2GB will be too big for int.MaxValue. I tried creating an extention method similar to this but MemoryStream's capacity is of type Int.......
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.