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.
Solution
Code:
@{
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);
}
}
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.