asp.net-mvc – asp.net mvc的Paperclip
发布时间:2020-12-16 07:27:26 所属栏目:asp.Net 来源:网络整理
导读:是否存在类似Paperclip for Rails的插件? 实现自己的上传文件系统真的很痛苦调整大
是否存在类似Paperclip for Rails的插件?
实现自己的上传文件系统真的很痛苦调整大小… 对于模型的属性来说会很酷,这样会得到params: 模型: [Paperclip(Sizes={thumb="100x20",big="200x40"},Path="~/public/")] public string Image{get;set;} 视图: Html.Editor(x=>x.Image) here is small tutorial for rails. 解决方法
看起来这个问题已经很老了,我偶然偶然发现它,因为它似乎还没有回答,我决定带上我的2¢.
我不知道ASP.NET是否存在这样的插件,但编写一个插件非常容易: [AttributeUsage(AttributeTargets.Property)] public class PaperClipAttribute : Attribute,IMetadataAware { public PaperClipAttribute(string uploadPath,params string[] sizes) { if (string.IsNullOrEmpty(uploadPath)) { throw new ArgumentException("Please specify an upload path"); } UploadPath = uploadPath; Sizes = sizes; } public string UploadPath { get; private set; } public string[] Sizes { get; private set; } public void OnMetadataCreated(ModelMetadata metadata) { metadata.AdditionalValues["PaperClip"] = this; } } 然后为HttpPostedFileBase类型定义自定义模型绑定器: public class PaperClipModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext) { var paperClip = bindingContext.ModelMetadata.AdditionalValues["PaperClip"] as PaperClipAttribute; if (paperClip == null) { return base.BindModel(controllerContext,bindingContext); } var uploadedFile = base.BindModel(controllerContext,bindingContext) as HttpPostedFileBase; if (uploadedFile == null) { return null; } var uploadPath = controllerContext.HttpContext.Server.MapPath(paperClip.UploadPath); if (!Directory.Exists(uploadPath)) { throw new ArgumentException(string.Format("The specified folder "{0}" does not exist",uploadPath)); } var sizes = (from size in paperClip.Sizes let tokens = size.Split('x') select new Size(int.Parse(tokens[0]),int.Parse(tokens[1])) ).ToArray(); foreach (var size in sizes) { var extension = Path.GetExtension(uploadedFile.FileName); var outputFilename = Path.Combine( uploadPath,Path.ChangeExtension( string.Format("image{0}x{1}",size.Width,size.Height),extension ) ); Resize(uploadedFile.InputStream,outputFilename,size); } return base.BindModel(controllerContext,bindingContext); } private void Resize(Stream input,string outputFile,Size size) { using (var image = Image.FromStream(input)) using (var bmp = new Bitmap(size.Width,size.Height)) using (var gr = Graphics.FromImage(bmp)) { gr.CompositingQuality = CompositingQuality.HighSpeed; gr.SmoothingMode = SmoothingMode.HighSpeed; gr.InterpolationMode = InterpolationMode.HighQualityBicubic; gr.DrawImage(image,new Rectangle(0,size.Height)); bmp.Save(outputFile); } } } 将在Application_Start中注册: protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); ModelBinders.Binders[typeof(HttpPostedFileBase)] = new PaperClipModelBinder(); } 我们已经完成了很多工作.剩下的就是经典之作. 查看型号: public class MyViewModel { [PaperClip("~/App_Data","100x20","200x40","640x480")] [Required(ErrorMessage = "Please select a file to upload")] public HttpPostedFileBase Image { get; set; } } 控制器: public class HomeController : Controller { public ActionResult Index() { return View(new MyViewModel()); } [HttpPost] public ActionResult Index(MyViewModel model) { if (!ModelState.IsValid) { return View(model); } return Content("thanks for uploading"); } } 视图: @model MyViewModel @using (Html.BeginForm(null,null,FormMethod.Post,new { enctype = "multipart/form-data" })) { @Html.LabelFor(x => x.Image) <input type="file" name="image" /> @Html.ValidationMessageFor(x => x.Image) <input type="submit" value="Upload" /> } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 深蓝词库转换2.2发布,支持手心输入法和Win10微软拼音
- ASP.NET -- 一般处理程序ashx
- asp.net core视图中使用Json序列化模型
- asp.net – Visual Studio加载项自动附加到Development Ser
- asp.net – 在IE9中HTML表格无法正确呈现
- asp.net – 无效的回发或回调参数.单击按钮时
- 实体框架 – 实体框架生成的类不在我需要的命名空间中,是否
- asp.net – 使用OpenOAuthProvider通过Google进行身份验证
- asp.net – 在.ashx处理程序中查看状态?
- asp.net – 使用Cookie认证的asp net core项目MVC控制器和使