ASP.NET MVC Upload file time out - c#

I currently have an ASP.NET MVC project that has file uploading and it works great if the user has a good enough connection and their file is of a reasonable size.
The problem I'm running into is that sometimes a user might have a 56k connection (how they can live with it in this day and age, I don't know) or are uploading a larger file or some combination of the two.
I'd like to keep a small timeout for normal pages (90 seconds or so), but allow for a larger timeout for actions where a user is uploading. This is just one action, so I don't mind putting code inside just that singular action rather than a generic solution.
Ultimately, a solution that would automatically increase the timeout if Request.Files.Count > 0 would be the best.

I'm not sure if this would work in an MVC project, but you could try creating a location in your web.config and set the execution timeout for just your upload URL. For example:
<location path="YourUrl">
<system.web>
<httpRuntime executionTimeout="9001"/>
</system.web>
</location>

You might need to increase the timeout in web.config:
<httpRuntime executionTimeout="01:00:00" />
Now this is overridable in sub web.config files meaning that if you want to increase the timeout only for the uploading script you could write a generic HTTP handler that will handle the uploads and put it in its own subfolder with its own web.config.

Possible issue: If its not a timeout because of the zero activity, maybe its something to do with the built in size restriction, in the web.config httpRuntime section you could add/increase maxRequestLength="" to your size limit

Related

Why is my MVC route not being found when I upload a large file?

I am using attribute routing for an MVC 5.2.3 project, and have run across a weird issue.
Users are able to upload files to the site, with a maximum file size of 3 MB. This functionality was and is working just fine. Recently, though, I realized that the size limit was only being enforced via JavaScript. So, I added some handling to check the size server-side before saving the file. I then removed the restriction on the front end, and uploaded a 5.26 MB file to test. To my relief, the upload was blocked. To my horror though, the response wasn't my nicely formatted error message, it was a 404.
Debugging with IIS Express, I found that for large files, the request doesn't even make it to the action.
On the other hand, uploading small files (tested up to 800KB) still works as expected. I can even decrease my coded-in limit to 1 KB, and successfully block my 800KB files with my nice error message.
Default IIS max request length seems to be about 30 MB. I tried upping that regardless, but no luck. I looked into the jQuery file upload library we use, but that supports files up to 4GB. I also tried recording the request with Fiddler, but couldn't find any obvious reasons for the larger one to fail. I also tried recording a successful file submission, and then replaying it with Fiddler's composer feature, replacing the file with the larger one. I got 404. One final oddity I haven't been able to explain, when looking at the Network tab in Firefox dev tools for a successful request, the Type column will be JSON. Failed/large requests show XML instead.
Why is my routing failing ONLY for large files?
Check out maxAllowedCOntentLength setting in web.config under system.webServerand set a max value you need (value in bytes):
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="104857600" />
</requestFiltering>
</security>
</system.webServer>
Roman.Pavelko's answer didn't end up being the issue, but it did get me on the right path.
There is another setting you can apply in the web.config as follows:
<system.web>
<httpRuntime maxRequestLength="xxxx" />
</system.web>
This value defaults to 4MB (in bytes). After upping it to 50MB, everything works like a charm!

How to time out request/connection

Recently, I've noticed that my website is getting hit by a crawler, which takes a very long time to open the pages. I never thought about it before but realized now that my MVC3 application never times out. For example, if I put in a Thread.Sleep(1000 * 60 * 10) (ten minutes) in my controller action and I open the page, after 10 minutes I will get rendered view.
I've read tons of articles and SO questions but no luck. I tried the solutions below on both localhost, and production server with "Release" built, but none of those did what I wanted it to do.
Solution 1:
In web.config:
<location path="ControllerName/ActionName">
<system.web>
<httpRuntime executionTimeout="1" />
</system.web>
</location>
Solution 2:
In the controller action
HttpContext.Server.ScriptTimeout = 1;
The only idea I had left was to calculate the time that elapsed since the request came in and compare it to current time and if it's bigger than my timeout limit, throw and TimeoutException() manually. I planned to put it in "OnActionExecution" and "OnActionExecuted" but if the request gets stuck somewhere in between those, I will never be able to tell if I should time it out.
Is there a good solution to implement this? Did anyone ever get request timeout to work in MVC3?
I don't think the timeout is a real problem here. I have a very strong suspicion that your site is hitting session locking issue, which is typical when being hammered by lots of simultaneous requests from the same source. Make sure you disable or mark session as readonly by default and only enable it on the actions where session is modified (like login controller for instance). See SessionStateAttribute for details.
Good answer here:
IIS Request Timeout on long ASP.NET operation
If you've already done this but are finding that your session is expiring then increase the ASP.NET HttpSessionState.Timeout value:
For example:
// Increase session timout to thirty minutes
Session.Timout = 30;
This value can also be configured in your web.config file in the sessionState configuration element:
<configuration>
<system.web>
<sessionState
mode="InProc"
cookieless="true"
timeout="30" />
</system.web>
</configuration>

Maximum request length exceeded error in file uploading

How to upload the 5mb pdf files using html upload button?when I try upload the 5mb files I get Maximum request length exceeded error?.This problem occurs because the default value for the maxRequestLength parameter in the section of the Machine.config file is 4096 (4 megabytes).So I try to change webconfig file,
<configuration>
<system.web>
<httpRuntime maxRequestLength="102400" executionTimeout="1200" />
</system.web>
</configuration>
If i use like this i got the An existing connection was forcibly closed by the remote host error.my project is hosted with IIS7.So I try to,
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1048576" />
</requestFiltering>
</security>
</system.webServer>
This way also not allow to upload the 5mb files?How to upload the 5 mb files through HTML Upload button?
trying adding a "0" To the end of your <requestLimits maxAllowedContentLength> value. I think it's in bytes, which means your example sets it to about 1MB.
Here's the doc on this setting.
If you are running this on a shared hosting account, chances are less that you will be able to resolve this. Due to limited resource allocation, hosting providers specify the timeout for a connection so if the connection takes too much time, it closes it.
My advice is, you should seek help of your hosting provider or try changing the host or plan.
Assuming this is your own server, if not see Murtuza Kabul.
Try
requestLengthDiskThreshold="800000"
Btw I would recommend NeatUpload because the HTML Upload isn't very good in my opinion.

Try to use File.SAVEAS but get 404 error, because the size is big

I'm using asp:fileupload control to upload my Picture files. So the user click on browse and select the file and click on upload and in event handler of upload button there is FileUpload.PostedFile.SaveAs () etc.
Everything works fine. Accept when for big file size. e.g. I've got a file (jpg) 5.5 MB. When I try to upload this file I get an the error below.
The strange thing is I the button upload file eventhandler I check the file size. If (intFileesize < intFileSizeLimit) etc.
But the strange thig is I remove all the code in Upload eventhandler for testing/debugging and I still get the error below. So the error occurs outside the Button handler. I mean
the error cause is not by Fileupload.SAveAs etc.... So the question is how can I avoid this. I mean I have built restriction of 1 mb, but This code is not reached.
I don't have any problems with small sizes e.g. I could upload 400 kb w/o problem.
So the question is what is the cause for the big file size how can I solve this?
Other question is: Is there a tool or whatever to crop the filesize and upload? I mean even if they upload 6 mb picture, I should crop that to 50kb or whatever during upload. How to aproach this? maybe a 3rd party freeware?
ERROR I get after 2-3 seconds
Oops! This page appears broken. HTTP 404 - File not found.
see maxRequestLength in http://msdn.microsoft.com/en-us/library/e1f13641.aspx
The default request size is likely being reached. By default I believe ASP.NET supports a 4MB request size limit. You can change this in your config:
<system.web>
<httpRuntime executionTimeout="240" maxRequestLength="20480" />
</system.web>
The above example changes the request size to 20MB. The thing to take into consideration is that HTTP was never really designed to handle large file uploads. You may want to consider using an alternative, such as a Flash upload control...
UPDATE
For IIS7 you may need to update the configuration in the system.webServer section:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="20480" />
IIS uses the same configuration system as ASP.NET, but because it may not be an ASP.NET handler being called, it has an additional setting for other content requests.
Since you already have maxRequestLength set in the <httpRuntime> tag in web.config, it looks like you are hitting the IIS7 request filter.
Try adding this to your web.config:
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="33554432"/>
</requestFiltering>
</security>
Depending on how your server is set up, you may also need to change the IIS configuration to allow you to set up request filtering at the application level rather than the machine level. Edit %windir%\system32\inetsrv\config\applicationHost.config and change:
<section name="requestFiltering" overrideModeDefault="Deny" />
To:
<section name="requestFiltering" overrideModeDefault="Allow" />

ASP.NET Configuration page

when I try to access the ASP.NET Configuration page from Visual Studio 2008, I fail . I get an error :
"An error was encountered. Please return to the previous page and try again.".
This is the message I get after clicking on Help : "Tool Has Timed Out . As a security measure, the Web Site Administration Tool times out after a period of inactivity. Changes to machine.config or web.config may also result in the tool needing to be restarted. To continue configuring your web site, restart the tool."
how can I solve this ?
Set the default browser within Visual Studio to internal browser, and attempt to re-launch the tool. This worked for me.
It's likely the is an error in your web.config file - try making sure the xml is valid, all tags are closed that sort of thing.
You might want to check the Event log for possibly more information on the actual underlying error
Edit your Web.config file and add this:
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>

Categories