您目前尚未登陆,请选择【登陆】或【注册
首页->博客论坛->SpaceBuilder v1.1源代码>>BlogControls/Admin/UploadBlogAttachment.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:SpaceBuilder v1.1源代码
当前文件:文件类型 SpaceBuider11/BlogControls/Admin/UploadBlogAttachment.cs打开代码结构图
普通视图
		            
1//------------------------------------------------------------------------------ 2// <copyright company="Tunynet"> 3// Copyright (c) Tunynet Inc. All rights reserved. 4// </copyright> 5//------------------------------------------------------------------------------ 6 7using System; 8using System.Collections.Generic; 9using System.Text; 10using SpaceBuilder.Components; 11using System.Web.UI.WebControls; 12using SpaceBuilder.Posts.Configuration; 13using SpaceBuilder.Posts.Components; 14using SpaceBuilder.Blogs.Configuration; 15using TunyNet.Utils; 16using SpaceBuilder.Blogs.Components; 17using SpaceBuilder.Controls; 18using SpaceBuilder.Controls.Utils; 19using SpaceBuilder.Configuration; 20using System.Drawing; 21using System.IO; 22using SpaceBuilder.Utils; 23 24namespace SpaceBuilder.Blogs.Controls 25{ 26 /// <summary> 27 /// 用户上传博客附件 28 /// </summary> 29 public class UploadBlogAttachments : WeblogAdminThemedControl 30 { 31 SBContext wlContext = SBContext.Current; 32 33 protected override void OnInit(EventArgs e) 34 { 35 if (SkinName == null) 36 SkinName = "Skin-UploadBlogAttachment.ascx"; 37 38 wlContext = SBContext.Current; 39 base.OnInit(e); 40 } 41 Child Controls 69 70 protected override void AttachChildControls() 71 { 72 tips = FindControl("Tips") as Literal; 73 blogAttachmentFile = FindControl("BlogAttachmentFile") as FileUpload; 74 blogAttachmentFileCustomValidator = FindControl("BlogAttachmentFileCustomValidator") as CustomValidator; 75 blogAttachmentSaveAsFileName = FindControl("BlogAttachmentSaveAsFileName") as TextBox; 76 statusMessage = FindControl("StatusMessage") as StatusMessage; 77 uploadBlogAttachmentButton = FindControl("UploadBlogAttachmentButton") as LinkButton; 78 uploadBlogAttachmentButton.Click += new EventHandler(uploadBlogAttachmentButton_Click); 79 } 80 81 void uploadBlogAttachmentButton_Click(object sender, EventArgs e) 82 { 83 if (!blogAttachmentFile.HasFile) 84 { 85 blogAttachmentFileCustomValidator.IsValid = false; 86 blogAttachmentFileCustomValidator.ErrorMessage = "没有选择要上传的文件"; 87 statusMessage.Visible = true; 88 statusMessage.Text = "没有选择要上传的文件"; 89 statusMessage.MessageType = StatusMessageType.Error; 90 return; 91 } 92 93 AttachmentSettings fs = WeblogConfiguration.Instance().AttachmentSettings; 94 if (fs.ValidateExtensions && !wlContext.User.IsBlogAdministrator) 95 { 96 if (!fs.ValidationRegex.IsMatch(blogAttachmentFile.FileName)) 97 { 98 string message = string.Format("只允许上传以下文件类型: {0}.", fs.Extensions.Replace(";", ",")); 99 blogAttachmentFileCustomValidator.ErrorMessage = message; 100 statusMessage.Visible = true; 101 statusMessage.MessageType = StatusMessageType.Error; 102 statusMessage.Text = message; 103 return; 104 } 105 } 106 if ((blogAttachmentFile.FileContent.Length / 1024) < WeblogConfiguration.Instance().AttachmentSettings.MaxAttachmentSize) 107 { 108 PostAttachment pa = new PostAttachment(blogAttachmentFile.PostedFile); 109 if (!ValueHelper.IsNullOrEmpty(blogAttachmentSaveAsFileName.Text)) 110 pa.FriendlyFileName = WebUtils.HtmlEncode(blogAttachmentSaveAsFileName.Text.Trim()); 111 112 pa.CreateDate = DateTime.Now; 113 pa.UserID = wlContext.User.UserID; 114 pa.PostID = 0; 115 pa.SectionID = CurrentWeblog.SectionID; 116 pa.ApplicationType = ApplicationType.Blog; 117 PostAttachments.Add(pa, ApplicationType.Blog); 118 119 //处理水印 120 if (pa.IsImage) 121 { 122 SiteSettings siteSettings = SiteSettingsManager.GetSiteSettings(); 123 if (siteSettings.WatermarkType != WatermarkType.None && siteSettings.EnableWatermarkForBlogAttachment) 124 { 125 if (pa.Width > siteSettings.WatermarkMinWidth && pa.Height > siteSettings.WatermarkMinHeight) 126 { 127 Bitmap image = (Bitmap)Bitmap.FromStream(pa.Content, true); 128 Stream newImageStream = null; 129 if (siteSettings.WatermarkType == WatermarkType.Text) 130 { 131 Watermark.AddWatermarkText(image, siteSettings.WatermarkText, siteSettings.WatermarkPosition, siteSettings.WatermarkAlpha, siteSettings.WatermarkQuality, out newImageStream); 132 } 133 else if (siteSettings.WatermarkType == WatermarkType.Image) 134 { 135 Watermark.AddWatermarkImage(image, siteSettings.WatermarkImagePhysicalPath, siteSettings.WatermarkPosition, siteSettings.WatermarkAlpha, siteSettings.WatermarkQuality, out newImageStream); 136 } 137 if (newImageStream != null) 138 pa.Content = newImageStream; 139 } 140 } 141 } 142 143 fs.SaveAttachmentToDisk(pa, pa.SectionID, pa.PostID); 144 145 TunyNet.Web.UI.ModalHelper.ClosePage(Page, "true"); 146 } 147 else 148 { 149 blogAttachmentFileCustomValidator.ErrorMessage = string.Format("上传附件不能超过{0}K", WeblogConfiguration.Instance().AttachmentSettings.MaxAttachmentSize); 150 statusMessage.Visible = true; 151 statusMessage.MessageType = StatusMessageType.Error; 152 statusMessage.Text = string.Format("上传附件不能超过{0}K", WeblogConfiguration.Instance().AttachmentSettings.MaxAttachmentSize); 153 } 154 155 } 156 157 protected override void OnLoad(EventArgs e) 158 { 159 base.OnLoad(e); 160 Header.AddTitle("上传博客附件", wlContext.Context); 161 } 162 163 protected override void OnPreRender(EventArgs e) 164 { 165 base.OnPreRender(e); 166 167 tips.Text = string.Format("请将您上传附件的大小限制在 {0} K<br/>支持上传的文件类型:{1}", WeblogConfiguration.Instance().AttachmentSettings.MaxAttachmentSize, WeblogConfiguration.Instance().AttachmentSettings.Extensions); 168 } 169 } 170} 171
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:SpaceBuilder v1.1源代码