上一篇已经介绍了百度的这个编辑器在.NET 项目中如何取值和赋值。
在接下来的使用中,发现图片上传不能用,看了下源文件,发现是调用PHP的,难怪!
于是经过半个小时的努力,把上传图片改成了.net的。源码如下,相信有C#基础的童鞋都看的懂的
在编辑器文件夹中的dialogs文件夹下的images下的文件夹中新建两个aspx页(Up.aspx和Upload.aspx),命名空间默认
Up.aspx页面代码
<html><head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <style type="text/css"> *{margin:0;padding:0} html,body{margin-top:-2px;} #filename{ position:absolute; z-index:9999; left:150px; opacity:0; filter:alpha(opacity=0); width:50px; height:21px; } #url{ position:absolute;left:0; width:146px;height:21px;background: #FFF;border:1px solid #d7d7d7;padding: 0; margin-top:-1px; } #flag{ position:absolute;left:150px; } .btn2 { border:0; background: url("../../themes/default/images/button-bg.gif") no-repeat; font-size:12px; height:23px; width:50px; text-align: center; cursor: pointer; } .btn1 { border:0; background: url("../../themes/default/images/button-bg.gif") 0 -27px no-repeat; font-size:12px; height:23px; width:50px; text-align: center; cursor: pointer; } </style> </head> <body> <form id="upImg" action="up.aspx" method="post" enctype="multipart/form-data" style="margin-top:5px;"> <input id="filename" name="filename" type="file" onmouseover="document.getElementById('flag').className='btn1'" onmouseout="document.getElementById('flag').className='btn2'" /> </form> </body></html>
Up.aspx.cs文件代码
public partial class up : System.Web.UI.Page { String uploadPath = "uploadfiles/"; //保存路径 String fileType = ".jpg,.jpeg,.gif,.png,.bmp"; //文件允许格式 Int32 fileSize = 1000; //文件大小限制,单位KB protected void Page_Load(object sender, EventArgs e) { //if (!IsPostBack) { HttpPostedFile oFile = Request.Files[0]; string fileExtension = System.IO.Path.GetExtension(oFile.FileName).ToLower(); if (fileType.ToLower().IndexOf(fileExtension) > -1)//检测是否为允许的上传文件类型 { if (this.fileSize * 1024 >= oFile.ContentLength) { try { string DirectoryPath; DirectoryPath = uploadPath + DateTime.Now.ToString("yyyy-MM"); string sFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff"); //文件名称 string FullPath = "~/" + DirectoryPath + "/" + sFileName + fileExtension;//最终文件路径 if (!Directory.Exists(Server.MapPath("~/" + DirectoryPath))) Directory.CreateDirectory(Server.MapPath("~/" + DirectoryPath)); oFile.SaveAs(Server.MapPath(FullPath)); Response.Write("<script type='text/javascript'>parent.reloadImg('" + Page.ResolveUrl(FullPath) + "');" + "location.href='upload.aspx?url=" + Page.ResolveUrl(FullPath) + "';</script>"); } catch (Exception ex) { WebHelper.AlertAndRedirect("上传文件失败。"+ex.Message, "upload.aspx"); } } else { WebHelper.AlertAndRedirect("上传文件大小超过限制。", "upload.aspx"); } } else { WebHelper.AlertAndRedirect("上传文件扩展名是不允许的扩展名。", "upload.aspx"); } } } }
Upload.aspx页面代码
<html> <head> <title></title> <style type="text/css"> *{margin:0;padding:0} html,body{margin-top:-2px;} #filename{ position:absolute; z-index:9999; left:150px; opacity:0; filter:alpha(opacity=0); width:50px; height:21px; } #url{ position:absolute;left:0; width:146px;height:21px;background: #FFF;border:1px solid #d7d7d7;padding: 0; margin-top:-1px; } #flag{ position:absolute;left:150px; } .btn2 { border:0; background: url("../../themes/default/images/button-bg.gif") no-repeat; font-size:12px; height:23px; width:50px; text-align: center; cursor: pointer; } .btn1 { border:0; background: url("../../themes/default/images/button-bg.gif") 0 -27px no-repeat; font-size:12px; height:23px; width:50px; text-align: center; cursor: pointer; } </style> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> </head> <body> <form id="upImg" action="up.aspx" method="post" enctype="multipart/form-data" style="margin-top:5px;"> <input type="hidden" id="path" name="path" /> <input id="filename" name="filename" type="file" onmouseover="document.getElementById('flag').className='btn1'" onmouseout="document.getElementById('flag').className='btn2'" /> <input id="url" type="text" name="url" readonly="readonly" value="<%=Url %>">" /> <input class="btn2" id="flag" name="flag" type="button" value="浏览…" onmouseover="this.className='btn1'" onmouseout="this.className='btn2'" onclick="sub()" /> </form> <script type="text/javascript"> var url = document.getElementById('url'); url.onkeydown = function (evt) { evt = event || evt; evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false); } var form = document.getElementById("upImg"); document.getElementById("filename").onchange = function () { //------------------------------------------ //如果需要上传功能,请取消以下两行注释即可!!!! //alert("由于安全原因,本demo暂不提供图片上传服务!下载包中包含了支持php版上传功能的相关文件,修改后即可使用。"); //return; //------------------------------------------ document.getElementById('path').value = this.value; form.submit(); } function sub() { var file = document.getElementById("filename"); if (file.click) file.click(); else if (file.fireEvent) file.fireEvent('onclick'); else if (document.createEvent) { var evt = document.createEvent("MouseEvents"); evt.initEvent("click", true, true); file.dispatchEvent(evt); } } </script> </body> </html>
Upload.aspx.cs页面代码
public partial class upload : System.Web.UI.Page { public String Url ="222<2&22"; protected void Page_Load(object sender, EventArgs e) { Url = filter(SiteFun.Query("Url")); } /// <summary> /// 字符过滤 /// </summary> /// <param name="Url"></param> /// <returns></returns> public String filter(String Url) { Url = Url.Replace("&", "&"); Url = Url.Replace("'", "&qpos;"); Url = Url.Replace(""", """); Url = Url.Replace("<", "<"); Url = Url.Replace(">", ">"); return Url; } }
最后,在同文件夹下的image.html文件夹中查找upload.php,替换成upload.aspx就OK了,测试 一下吧。。。
by 小火柴
哥哥,我觉得你应该再写个“三”了
@Jon:马上就写,呵呵
点击浏览 输入框里显示那个图片了。然后确定就会报“地址不正确”,怎么解决啊
@aspenfy:加断点调试一下,看看哪里错了
@小火柴:加了断点的,取到的url都是“”
@aspenfy:我这里当时测试的是可以,你出的错我也看不到,所以不敢断定哪里错了。你点我博客右上的QQ聊天把出错的截图发过来
有没有ASP的上传的代码?
@zver:木有,俺不会VB,不懂ASP