/// <summary> /// Valiate the upload file is allowed to be save /// </summary> /// <param name="context">The HTTP Context</param> /// <param name="postedFile">The uploaded file</param> /// <param name="fileName">The destination file name</param> /// <param name="fileType">The destination file MIME type</param> /// <param name="fileSize">The file full size (in multipart upload it can be that the postedFile is not in the same size)</param> /// <param name="errorMessage">An error message related to the vertification process</param> /// <returns>Boolean value that indicates if we can save the file</returns> public virtual bool ValidateUploadedFile(HttpContext context, HttpPostedFile postedFile, string fileName, string fileType, long fileSize, out string errorMessage) { errorMessage = string.Empty; //------------------------------------------- // Do we exceed the IIS post size? //------------------------------------------- if (fileSize > this.GetIISMaxAllowedPostSize() * 1024) { errorMessage = GetResourceString(context, "PostMaxSizeExceeded"); return false; } //------------------------------------------- // Is our file matching the allowed files pattern? //------------------------------------------- if (!this.AcceptedFilesPattern.IsMatch(fileName)) { errorMessage = GetResourceString(context, "InvalidFileType"); return false; } //------------------------------------------- // Check file size //------------------------------------------- if (this.FileMaxSize > 0 && fileSize > this.FileMaxSize) { errorMessage = GetResourceString(context, "FileMaxSizeExceeded", ((long)this.FileMaxSize).ToFileSize()); return false; } if (this.FileMinSize > 0 && fileSize < this.FileMinSize) { errorMessage = GetResourceString(context, "FileMinSizeNotReached", ((long)this.FileMinSize).ToFileSize()); return false; } //------------------------------------------- // We're exceeding the maximum number of files? //------------------------------------------- if (this.MaximumNumberOfFiles > -1 && this.CountAvailableFiles(context) > this.MaximumNumberOfFiles) { errorMessage = GetResourceString(context, "MaximumNumberOfFilesExcceded", this.MaximumNumberOfFiles.ToString()); return false; } //------------------------------------------- // We DO got an image, don't we? //------------------------------------------- if (!postedFile.IsImage()) { errorMessage = "NotValidImage"; return false; } //------------------------------------------- // Image dims vertification //------------------------------------------- if (this.ImageMinAllowedWidth > -1 || this.ImageMinAllowedHeight > -1 || this.ImageMaxAllowedWidth > -1 || this.ImageMaxAllowedHeight > -1) { /* Note: we're not using try-catch block * because we do know that we can instantiate Bitmap from the input stream * since we've already done it in IsImage() extension method. */ using (Bitmap image = new Bitmap(postedFile.InputStream)) { if (this.ImageMinAllowedWidth > image.Width) { errorMessage = GetResourceString(context, "ImageMinAllowedWidthNotMet", this.ImageMinAllowedWidth.ToString()); return false; } if (this.ImageMinAllowedHeight > image.Height) { errorMessage = GetResourceString(context, "ImageMinAllowedHeightNotMet", this.ImageMinAllowedHeight.ToString()); return false; } if (this.ImageMaxAllowedWidth < image.Width) { errorMessage = GetResourceString(context, "ImageMaxAllowedWidthExceeded", this.ImageMaxAllowedWidth.ToString()); return false; } if (this.ImageMaxAllowedHeight < image.Height) { errorMessage = GetResourceString(context, "ImageMaxAllowedHeightExceeded", this.ImageMaxAllowedHeight.ToString()); return false; } } } return true; }