protected override void OnLoad(EventArgs e) { if (HttpContext.Current != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Params["JsonId"]) && HttpContext.Current.Request.Params["JsonId"].Equals(this.JsonId)) { HttpRequest request = HttpContext.Current.Request; HttpResponse response = HttpContext.Current.Response; try { HttpPostedFile file = request.Files["file"]; string filepath = file.FileName; string filename = Path.GetFileName(filepath); string path = HttpContext.Current.Server.MapPath(this.VirtualUploadPath + filename); double kilobyteSize = ByteSizeConverter.BytesToKilobytes(file.ContentLength); if (kilobyteSize > this.MaxSize) { throw new MaxFileSizeExceededException(this.MaxSize, kilobyteSize); } else { using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, file.ContentLength)) { byte[] fileBytes = new byte[file.ContentLength]; file.InputStream.Read(fileBytes, 0, file.ContentLength); fs.Write(fileBytes, 0, file.ContentLength); FileUpload dbEntry = FileUpload.New(this.ApplicationDatabase); dbEntry.File = fileBytes; dbEntry.FileName = filename; dbEntry.UploadedBy = UserUtil.GetCurrentUser(true); dbEntry.UploadDate = DateTime.UtcNow; dbEntry.Size = Convert.ToInt64(file.ContentLength); dbEntry.FullName = filepath; dbEntry.ContentType = file.ContentType; //dbEntry.Description = // not recording this info yet if (dbEntry.Insert() == -1) { LogManager.CurrentLog.AddEntry("An error occurred inserting new FileUpload record (fileName={0},fileSize={1})", dbEntry.LastException, filename, kilobyteSize.ToString()); } } WriteSuccess("File uploaded successfully."); this.OnUploadComplete(path); } } catch (MaxFileSizeExceededException mfsee) { LogManager.CurrentLog.AddEntry("An error occurred uploading/receiving file, max size {0} Kb, file size {1} Kb.", LogEventType.Warning, mfsee.AllowedSize.ToString(), mfsee.AttemptedSize.ToString()); WriteError(string.Format("File is too large, maximum size allowed is {0}.", mfsee.AllowedSize)); this.OnUploadError(mfsee); } catch (Exception ex) { LogManager.CurrentLog.AddEntry("An error occurred uploading/receiving file: {0}", ex, ex.Message); WriteError("An error occurred uploading your file. We are working to correct the problem and apologize for the inconvenience. Please try again in a few minutes."); this.OnUploadError(ex); } } else { base.OnLoad(e); } }