Post by dejohnny on Jun 3, 2012 19:52:12 GMT -5
I'm puzzled by what's happening when I try to upload a file larger than 4MB.
The code worked as expected, but when I modified the code to use _PageStart.cshtml, all hell broke loose! Instead of getting a nice error page describing the error that was thrown, I'm getting the usual error page that IIS7 displays. What am I missing?
Here are the steps I've gone through to run the site:
Select and run Default.cshtml.
Choose a file larger than 4MB.
Click Upload button and wait.
Result: Standard IIS7 error page.
IIS7 Error page
Solution
It looks like I would need to perform a sanity check on any file that gets uploaded then.
We could use the Request object property TotalBytes, to check for the size of the HttpRequest before it gets uploaded.
Also, in this instance, I will not be using _PageStart.cshtml to trap errors from web pages. It doesn't work as expected. I think it's better to handle exceptions on each page individually and then send the errors by calling one function that takes care of logging and emailing.
Razor C# code snippet
The only problem with this approach is that, WebImage objects only detect the properties of binary filestreams. This will not work, if for example, you accidentally upload a PDF or exe file. IIS7 will baulk at you with a 404.13 error page if you do that.
How can I prevent other file types in the HttpRequest stream from being processed?
The code worked as expected, but when I modified the code to use _PageStart.cshtml, all hell broke loose! Instead of getting a nice error page describing the error that was thrown, I'm getting the usual error page that IIS7 displays. What am I missing?
Here are the steps I've gone through to run the site:
Select and run Default.cshtml.
Choose a file larger than 4MB.
Click Upload button and wait.
Result: Standard IIS7 error page.
IIS7 Error page
HTTP Error 404.13 - Not Found
The request filtering module is configured to deny a request that exceeds the request content length.
Most likely causes:
Request filtering is configured on the Web server to deny the request because the content length exceeds the configured value.
Things you can try:
Verify the configuration/system.webServer/security/requestFiltering/requestLimits@maxAllowedContentLength setting in the applicationhost.config or web.config file.
The request filtering module is configured to deny a request that exceeds the request content length.
Most likely causes:
Request filtering is configured on the Web server to deny the request because the content length exceeds the configured value.
Things you can try:
Verify the configuration/system.webServer/security/requestFiltering/requestLimits@maxAllowedContentLength setting in the applicationhost.config or web.config file.
Solution
It looks like I would need to perform a sanity check on any file that gets uploaded then.
We could use the Request object property TotalBytes, to check for the size of the HttpRequest before it gets uploaded.
Also, in this instance, I will not be using _PageStart.cshtml to trap errors from web pages. It doesn't work as expected. I think it's better to handle exceptions on each page individually and then send the errors by calling one function that takes care of logging and emailing.
Razor C# code snippet
@{
var pageTitle = "Default Page";
var message = "";
try{
/// upload the image, if the byte stream
/// is less than 3MB
if(Request.TotalBytes < 3145728 ){
if(Request.Files.Count > 0 ){
/// WebImage class only understands the
/// properties of binary images
WebImage image = WebImage.GetImageFromRequest();
if(image != null){
message = "Upload succeeded";
} else {
message = "You must upload an image file: .jpg, .gif, .png, .bmp, .tif";
}
}
}
else {
message = "You cannot upload files larger than 3MB!";
}
}
catch(Exception ex){
message = "Upload unsuccessful because: " + ex.Message;
/// Log errors and send email notifications when errors occur
Functions.debugNotify( ex, message, pageTitle);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<!-- Render the variable values to the page. -->
<!-- @@objectinfo.Print(message) -->
<h1>Upload Images</h1>
<div>@message</div> <div>@request.TotalBytes bytes</div><br/>
@fileupload.GetHtml(allowMoreFilesToBeAdded: false)
</body>
</html>
The only problem with this approach is that, WebImage objects only detect the properties of binary filestreams. This will not work, if for example, you accidentally upload a PDF or exe file. IIS7 will baulk at you with a 404.13 error page if you do that.
How can I prevent other file types in the HttpRequest stream from being processed?