目录javascript
文件的上传和路径处理必须解决下面列出的实际问题:html
1.重复文件处理前端
2.单独文件上传java
3.编辑器中文件上传jquery
4.处理文章中的图片路径web
5.处理上传地址的变化算法
文件处理的原则是:不在数据库中保存文件,只在数据库中保存文件信息(Hash值等)。采起文件的MD5重命名文件在通常状况足够处理文件的重复问题,强迫症倾向则能够考虑将MD5和其余摘要算法结合。数据库
public static string Save(HttpPostedFileBase file, string path) { var root = "~/Upload/" + path + "/"; var phicyPath = HostingEnvironment.MapPath(root); Directory.CreateDirectory(phicyPath); var fileName = Md5(file.InputStream) + file.FileName.Substring(file.FileName.LastIndexOf('.')); file.SaveAs(phicyPath + fileName); return fileName; }
网站Logo、分类图标等各类场景须要单独文件上传的处理。经过使用UIHintAttribute或自定义继承自UIHintAttribute的特性咱们将文件上传的前端逻辑的重复代码消灭,使用统一的视图文件处理。曾经使用过Uplodify和AjaxFileUploader,前者存在flash依赖和cookie问题,后者基本已通过时。此处咱们采用KindEditor中的文件上传组件做为演示。非Flash的支持IE6+的方案的核心都是经过iframe方式实现伪AJax上传,核心仍是经过html form post到服务器。缓存
public class UploadModel { [Display(Name = "图标")] [UIHint("Upload")] public string Image { get; set; } [Display(Name = "简单模式")] [UIHint("Editor")] [AdditionalMetadata("useSimple", true)] public string Text1 { get; set; } [Display(Name = "标准模式")] [UIHint("Editor")] public string Text2 { get; set; } }
在咱们的实际项目中采起继承UIHintAttribute的方式,其中的path路径指定存储的下级地址,相似的还有DropDownAttribute、EditorAtrribute等等。仅供参考。服务器
[AttributeUsage(AttributeTargets.Property)] public class UploadAttribute : UIHintAttribute, IMetadataAware { public string Path { get; private set; } public UploadAttribute(string path = "") : base("Upload") { this.Path = path; } public virtual void OnMetadataCreated(ModelMetadata metadata) { metadata.AdditionalValues.Add("Path", this.Path); } }
Razor:在Shared中添加EditorTemplates文件夹,新建Upload.cshtml文件。
<script> KindEditor.ready(function (K) { var editor = K.editor({ allowFileManager: false, allowImageUpload: true, formatUploadUrl: false, uploadJson: '@url', }); K('#btn_@id').click(function () { editor.loadPlugin('insertfile', function () { editor.plugin.fileDialog({ fileUrl: K('#@id').val(), clickFn: function (url, title) { K('#@id').val(url); $('#image_@id').attr('src', url); editor.hideDialog(); } }); }); }); }); $('#rest_@id').click(function () { $('#@id').attr('value', ''); $('#image_@id').attr('src', '@Url.Content("~/Images/default.png")'); }); </script>
编辑器中的文件上传和单独文件上传的主要区别是上传后返回值的处理,编辑器须要将url插入到编辑的位置。编辑器采用过CKeditor和UMeditor,二者都须要我改源代码才能处理路径问题。上传地址和返回值的配置若是不能方便的视图中调整的编辑器,我我的不认为是好编辑器,这就比如一个类库无法扩展和自定义配置同样。仍然采用KindEditor做为演示。Editor.cshtml的主要内容以下:
<script type="text/javascript"> var editor; KindEditor.ready(function (K) { editor = K.create('textarea[name="@Html.IdForModel()"]', { resizeType: 1, allowPreviewEmoticons: false, allowImageUpload: true, uploadJson: '@UploadManager.UploadUrl', formatUploadUrl: false, allowFileManager: false @if(useSimple) { <text>, items: [ 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', '|', 'emoticons', 'image', 'link'] </text> } }); }); </script>
重头戏来了,这个看似问题能够回避,其实真的没法回避。更换目录、域名和端口,使用子域名或其余域名做为图片服务器等等,这些状况让咱们必须处理好这个问题,不然往后会浪费更多的时间。这不是小问题,打开支持插入图片的各个网站的编辑器,查看一下图片的路径,大可能是绝对url的,又或者只基于根目录的。若是你以产品的形式提供给客户,更不可能要求客户本身挨个替换文章中的路径了。
1.在数据库中不存储文件路径,使用URL路径做为存储。
2.使用html base元素解决相对路径的引用问题。
就是base元素,可能有的人认为这个base无关紧要,但在处理图片路径的问题上,没有比base更简洁更优雅的方案了。至少我没有也没找到过。其实能够把所有的静态资源都移除到外部存储,若是你须要。在测试时,咱们切换回使用本地存储。
@{ var baseUrl = UploadManager.UrlPrefix; } <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" /> <title>@ViewBag.Title</title> <base href="@baseUrl" /> <script src="~/Scripts/jquery-1.11.2.min.js"></script> @RenderSection("head",false) </head> <body> @RenderBody() </body> </html>
咱们须要独立的图片服务器处理上传或者使用第三方的图片存储服务时,咱们的上传地址改变了,若是刚刚提到的图片路径同样,所以咱们将上传路径和图片路径都采起配置的方式方便更改,咱们就曾经切换到又拍云又切换到自有的服务器。在个人实际使用时配置在数据中使用时采用缓存。为了便于演示咱们直接使用配置文件。
首先定义配置文件的处理程序
public class UploadConfig : IConfigurationSectionHandler { public object Create(object parent, object configContext, System.Xml.XmlNode section) { var config = new UploadConfig(); var urloadUrlNode = section.SelectSingleNode("UploadUrl"); if (urloadUrlNode != null && urloadUrlNode.Attributes != null && urloadUrlNode.Attributes["href"] != null) { config.UploadUrl = Convert.ToString(urloadUrlNode.Attributes["href"].Value); } var urlPrefixNode = section.SelectSingleNode("UrlPrefix"); if (urlPrefixNode != null && urlPrefixNode.Attributes != null && urlPrefixNode.Attributes["href"] != null) { config.UrlPrefix = Convert.ToString(urlPrefixNode.Attributes["href"].Value); } return config; } public string UploadUrl { get; private set; } public string UrlPrefix { get; private set; } }
在web.config中配置
<configSections> <section name="UploadConfig" type="SimpleFileManager.UploadConfig, SimpleFileManager" requirePermission="false" /> </configSections> <UploadConfig> <UploadUrl href="~/File/Upload/" /> <UrlPrefix href="~/Upload/" /> </UploadConfig>
使用UploadMange缓存和管理配置
public static class UploadManager { private static string uploadUrl; private static string urlPrefix; static UploadManager() { var config = ConfigurationManager.GetSection("UploadConfig") as UploadConfig; var url = config != null && !string.IsNullOrEmpty(config.UploadUrl) ? config.UploadUrl : "~/File/Upload"; uploadUrl = url.StartsWith("~") ? UploadHelper.GetUrlFromVisualPath(url) : url; var prefix = config != null && !string.IsNullOrEmpty(config.UrlPrefix) ? config.UrlPrefix : "~/Upload"; urlPrefix = prefix.StartsWith("~") ? UploadHelper.GetUrlFromVisualPath(prefix) : prefix; } public static string UploadUrl { get { return uploadUrl; } } public static string UrlPrefix { get { return urlPrefix; } } }
文件Hash的Md五、返回值的Json处理、完整URL的生成和文件的保存这些具体技术的依赖为了便于演示,统一放置在UploadHelper中,由于这些不是重点。实际应用中能够采起接口隔离并经过IoC注入的方式解耦。