コード例 #1
0
        /// <summary>
        /// Present as a link, followed by a span with format and size info
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
        protected override void OnPreRender(EventArgs e)
        {
            if (this.AttachmentUrl == null)
            {
                this.Visible = false;
                return;
            }

            if (Resource != null && Resource.Values.ContainsKey("umbracoExtension"))
            {
                if (!AttachmentType.ToString().ToUpperInvariant().StartsWith(Resource.Values["umbracoExtension"].ToUpperInvariant(), StringComparison.Ordinal))
                {
                    this.Visible = false;
                    return;
                }
            }

            using (var presentationContainer = new HtmlGenericControl("span"))
            {
                if (!String.IsNullOrEmpty(CssClass))
                {
                    presentationContainer.Attributes["class"] = CssClass;
                }
                this.Controls.Add(presentationContainer);

                // AttachmentText property holds an string identifier for a .NET RESX resource
                StringBuilder resKey = new StringBuilder("FormAttachment");
                resKey.Append(this.AttachmentType);

                using (HtmlAnchor link = new HtmlAnchor())
                {
                    link.HRef                = this.AttachmentUrl.ToString();
                    link.InnerText           = this.resManager.GetString(resKey.ToString());
                    link.Attributes["class"] = "no-meta";

                    resKey.Append("Title");
                    StringBuilder linkTitle = new StringBuilder(this.resManager.GetString(resKey.ToString()));
                    if (linkTitle != null && linkTitle.Length > 0)
                    {
                        link.Title = linkTitle.Append(" (").Append(this.resManager.GetString("Format" + this.attachmentType)).Append(")").ToString();
                    }
                    presentationContainer.Controls.Add(link);
                }

                // display the file size in brackets
                string size = "";
                if (Resource != null)
                {
                    size = CmsUtilities.GetResourceFileSize(Resource);
                }
                if (size.Length > 0)
                {
                    using (HtmlGenericControl sizeElement = new HtmlGenericControl("span"))
                    {
                        sizeElement.InnerHtml = new StringBuilder().Append(" (").Append(this.resManager.GetString("Format" + this.attachmentType.ToString())).Append(" &#8211; <span class=\"downloadSize\">").Append(size).Append("</span>)").ToString();
                        sizeElement.Attributes.Add("class", "downloadDetail");
                        presentationContainer.Controls.Add(sizeElement);
                    }
                }

                this.Visible = true;
            }
        }
コード例 #2
0
        protected override void OnPreRender(EventArgs e)
        {
            if (Resource != null)
            {
                // get file extension
                string format = Resource.Values["umbracoExtension"].ToLower(CultureInfo.CurrentCulture);

                // start with icon for file format
                using (HtmlGenericControl also = new HtmlGenericControl("span"))
                {
                    also.Attributes.Add("class", "downloadAlso");
                    also.InnerText = "Also in: ";
                    this.Controls.Add(also);
                }

                // link to file using name of format
                using (HtmlAnchor link = new HtmlAnchor())
                {
                    link.Title = "View "; // default action
                    switch (format)
                    {
                    case "rtf":
                        link.InnerText          = "Rich text";
                        link.Attributes["type"] = "application/rtf";
                        break;

                    case "doc":
                        link.InnerText          = "Word";
                        link.Attributes["type"] = "application/msword";
                        break;

                    case "xls":
                        link.InnerText          = "Excel";
                        link.Attributes["type"] = "application/excel";
                        break;

                    case "pdf":
                        link.InnerText          = "Acrobat (PDF)";
                        link.Attributes["type"] = "application/pdf";
                        break;

                    case "ppt":
                        link.InnerText          = "PowerPoint";
                        link.Attributes["type"] = "application/powerpoint";
                        break;

                    case "mp3":
                        link.InnerText          = "MP3";
                        link.Attributes["type"] = "audio/mpeg3";
                        link.Title = "Listen to ";     // change action
                        break;

                    case "wma":
                        link.InnerText          = "Windows Media";
                        link.Attributes["type"] = "audio/x-ms-wma";
                        link.Title = "Listen to ";     // change action
                        break;

                    case "xml":
                        link.InnerText          = "XML";
                        link.Attributes["type"] = "text/xml";
                        break;

                    default:
                        link.InnerText = "Alternative format";
                        break;
                    }
                    link.Attributes["class"] = format + " no-meta";
                    link.HRef = AttachmentUrl.ToString();
                    string docTitle = "'" + Resource.Name + "'";
                    link.Title += docTitle + " in " + link.InnerText + " format";
                    this.Controls.Add(link);


                    // display the file size in brackets
                    string size = CmsUtilities.GetResourceFileSize(Resource);
                    if (size.Length > 0)
                    {
                        using (var sizeElement = new HtmlGenericControl("span"))
                        {
                            ;
                            sizeElement.InnerText = " (" + size + ")";
                            sizeElement.Attributes.Add("class", "downloadSize");
                            link.Controls.Add(sizeElement);
                        }
                    }
                }
                this.Visible = true;
            }
            else
            {
                this.Visible = false;
            }
        }