/// <summary> /// Saves the uploaded file. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void addFile_Click(object sender, EventArgs e) { if (this.FreeFileDataSlotExists()) { bool FileSaveRequestIsValid = this.ValidFileSaveRequest(); if (this.fileBrowserBox.PostedFile != null && this.fileBrowserBox.PostedFile.ContentLength > 0 && FileSaveRequestIsValid) { // Get the user requesting the delete operation AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); string modifiedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name; // Read the file details and data into a file object string fileDescription = null; if (this.fileDescriptionBox != null) { fileDescription = this.fileDescriptionBox.Text; } DatabaseFileData fileData = new DatabaseFileData(this.fileBrowserBox.PostedFile, fileDescription); // Store the file in the database. int recordId = this.SaveFileInDB(fileData, modifiedBy); if (recordId > 0) { // We now need to store this new id and the filename in the file data arrays. this.AddFileDataToArrays(recordId, fileData.FileOriginalName); } } } }
/// <summary> /// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event. /// </summary> /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param> protected override void OnPreRender(EventArgs e) { // In postbacks, the controls are updated from the viewstate at this point. // Cycle through each file id in the field data array. If a file id exists then show the file details on screen. for (int index = 0; index < maxFiles; index++) { HtmlInputHidden idBox = this.fileIdArray[index]; HtmlInputHidden nameBox = this.fileNameArray[index]; HtmlGenericControl spanFilename = this.spanFilenameArray[index]; HtmlGenericControl divFile = this.divFileArray[index]; if (!string.IsNullOrEmpty(idBox.Value)) { int FileDataID = Convert.ToInt32(idBox.Value); // Get just the file details without the attachment body DatabaseFileData fileData = GetFileFromDB(FileDataID, false); // Create a file link control FileLinkControl fileLink = new FileLinkControl(); // Feed it a file handler url, but manually override the file extension detail switch (this.attachmentType) { case MultiFileAttachmentType.Document: fileLink.NavigateUrl = GetFileAttachmentUrl(FileDataID, this.dotnetProjectName); break; case MultiFileAttachmentType.Image: fileLink.NavigateUrl = GetImageUrl(FileDataID, this.dotnetProjectName); break; } fileLink.FileExtension = System.IO.Path.GetExtension(fileData.FileOriginalName); fileLink.FileSize = fileData.FileSize; // Note: the filename in hidden storage (nameBox.Value) and the filename extracted from the database will be the same (fileData.FileOriginalName). fileLink.InnerText = nameBox.Value; // Now add the file link control spanFilename.Controls.Add(fileLink); } // Show or hide the file display divFile.Visible = !string.IsNullOrEmpty(idBox.Value); } base.OnPreRender(e); }
/// <summary> /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface. /// </summary> /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param> public void ProcessRequest(HttpContext context) { this.Context = context; int fileDataID = this.FileDataID; if (fileDataID > 0) { // We have a file we need to retrieve from the database DatabaseFileData fileData = GetFileAttachment(fileDataID, true); // Tell the response object the real name for the file this.Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileData.FileOriginalName); // Tell the response object the content type for the file this.Context.Response.ContentType = fileData.FileContentType; // Now write the file's binary data out to the response object this.Context.Response.BinaryWrite(fileData.FileBLOBData); } }
/// <summary> /// Saves the uploaded file to the database /// </summary> /// <param name="fileData"></param> /// <param name="modifiedBy"></param> /// <returns>The record id for the stored file</returns> /// <remarks>This has to be abstract to enable this control to be generic.</remarks> protected abstract int SaveFileInDB(DatabaseFileData fileData, string modifiedBy);