/// <summary> /// This will populate the server controls and show the image preview popup. /// </summary> protected void btnDocumentPreviewLaunch_Click(object sender, EventArgs e) { if (!PageBase.StopProcessing) { //This hidden field contains the value populated from the client side. int documentMediaId = 0; int.TryParse(hdnDocumentMediaId.Value, out documentMediaId); if (documentMediaId > 0) { DocumentMedia media = GetBL <UtilityBL>().GetDocumentMedia(documentMediaId); if (media != null && Support.CanAccessMedia(media, DataContext)) { DocumentMediaId = documentMediaId; var itemBriefItemDocumentMedia = DataContext.ItemBriefItemDocumentMedias.Where(ibi => ibi.ItemBriefDocumentMediaId == DocumentMediaId).FirstOrDefault(); imgPreview.Src = ResolveUrl(Support.GetImageFilePreviewUrl(documentMediaId, PREVIEW_SIZE)); lnkDownload.HRef = ResolveUrl(Support.GetImageFileDownloadUrl(documentMediaId)); txtName.Text = media.Name; string fileType = "image"; if (ImageHelper.IsImageFileType(media.FileExtension)) { lblDocumentExtension.Visible = false; popupDocumentPreview.Title = "Preview Image"; lnkDownload.Title = "Download full size image"; popupDocumentPreviewRemoveConfirmation.Title = "Remove Image"; documentLabelWatermark.WatermarkText = "Click to set image label"; } else { lblDocumentExtension.Visible = true; lblDocumentExtension.Text = media.FileExtension; popupDocumentPreview.Title = "File Properties"; lnkDownload.Title = "Download File"; fileType = "file"; popupDocumentPreviewRemoveConfirmation.Title = "Remove File"; documentLabelWatermark.WatermarkText = "Click to set file label"; } if (itemBriefItemDocumentMedia != null) { ltrlRemoveConfirmText.Text = string.Format("Deleting this {0} will also remove it from the Complete Item tab. <br /> Are you sure you want to remove this {0}?", fileType); } else { ltrlRemoveConfirmText.Text = string.Format("Are you sure you want to remove this {0}?", fileType); } popupDocumentPreview.ShowPopup(); } } } }
/// <summary> /// Loads the file data specified by the 'documentMediaId' and sends it to the browser. /// </summary> private void SendMediaFromDB() { try { DocumentMedia media = GetBL <UtilityBL>().GetDocumentMedia(DocumentMediaId); if (Support.CanAccessMedia(media, DataContext)) { //If a media name has not been specified, use the media ID as the file name. string fileName = (media.Name == null) ? media.DocumentMediaId.ToString() : media.Name; fileName = fileName.Replace("\"", string.Empty); if (media.IsImageFile) { byte[] bytes = null; #region Get image bytes //If a specific size has not been specified, get the thumbnail or full content. //Otherwise send the resized image. if (Size == 0) { bytes = IsThumbnail ? media.Thumbnail : media.DocumentMediaContent; } else { using (MemoryStream ms = new MemoryStream(media.DocumentMediaContent)) { bytes = ImageHelper.GetResizedImage(ms, Size, ImageHelper.GetImageFormat(media.FileExtension)); } } #endregion Get image bytes if (bytes != null) { using (MemoryStream ms = new MemoryStream(bytes)) using (Image image = Image.FromStream(ms)) { WriteImageResponse(image, fileName, media.FileExtension); return; } } } else { if (IsDownload) { WriteBinaryResponse(media.DocumentMediaContent, fileName, media.FileExtension); return; } else { using (Image image = Image.FromFile(GetDocumentIconUrl(media.FileExtension))) { WriteImageResponse(image, "icon", "png"); return; } } } } } catch { } //If any error occurs, or permission is denied, send the palceholder image to the browser. using (Image image = Image.FromFile(Server.MapPath(placeholderThumbUrl))) { WriteImageResponse(image, "noimage", "png"); return; } }