ASP.NET MVC框架中引入JQUERY JQRTE控件
經(jīng)過(guò)將近兩周的努力,終于將JQUERY的JQRTE文本編輯器控件引入到了asp.net mvc框架中,主要步驟如下:
1.在asp.net mvc項(xiàng)目中引入jqrte類(lèi)庫(kù),聲明輔助類(lèi)用于存儲(chǔ)服務(wù)器端上載文件的信息
- public class ViewDataUploadFilesResult
- {
- public string message { get; set; }
- //public int Length { get; set; }
- public string imagepath { get; set; }
- public string error { get; set; }
- }
2.編寫(xiě)處理文件上載服務(wù)器段代碼,并將上載的文件信息返回給客戶(hù)端,代碼如下:
- [AcceptVerbs(HttpVerbs.Post)]
- public JsonResult UploadFiles(FormCollection collection)
- {
- var r = new ViewDataUploadFilesResult();
- foreach (string file in Request.Files)
- {
- string url = Request.Url.Authority;
- url = "http://" + url;
- HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
- string date = DateTime.Now.Date.ToShortDateString();
- string path = Path.Combine(
- AppDomain.CurrentDomain.BaseDirectory,
- "Content");
- string datePath = Path.Combine(path,date);
- Directory.CreateDirectory(datePath);
- url += "/Content/";
- url += date;
- url += "/";
- url += Path.GetFileName(hpf.FileName);
- if (hpf.ContentLength == 0)
- continue;
- string savedFileName = Path.Combine(
- datePath,
- Path.GetFileName(hpf.FileName));
- try
- {
- hpf.SaveAs(savedFileName);
- }
- catch (Exception e)
- {
- r.error = e.ToString();
- }
- //r.Name = savedFileName;
- //r.Length = hpf.ContentLength;
- r.imagepath = url;
- r.message = "ok";
- r.error = "ok";
- //r.Add(new ViewDataUploadFilesResult()
- //{
- // Name = savedFileName,
- // Length = hpf.ContentLength
- //});
- }
- JsonResult jsonResult = Json(r);
- jsonResult.ContentType = "text/html";
- return jsonResult;
- }
之所以搞了這么長(zhǎng)時(shí)間,問(wèn)題也主要出在這兒,開(kāi)始用的是return json(r),發(fā)現(xiàn)jquery的回調(diào)函數(shù)總是無(wú)法獲得服務(wù)器端的json,反而會(huì)跳出個(gè)下載文件對(duì)話(huà)框,反復(fù)閱讀jquery的源代碼,折磨了一周多后,在asp.net mvc官方論壇上注冊(cè)了個(gè)用戶(hù),經(jīng)過(guò)一番討論,最后發(fā)現(xiàn)對(duì)于有file控件的ajax form,在action方法中應(yīng)當(dāng)制定json的contentType才會(huì)正確處理json對(duì)象,源代碼如上,感謝熱心朋友的幫助,要不然不知道這個(gè)問(wèn)題會(huì)折磨到我什么時(shí)候。原貼鏈接如下:http://forums.asp.net/t/1439867.aspx
3.修改jqrte的fileupload源代碼,只要改一下action路徑就行,在jquery.jqrte.min.js中修改uploads函數(shù):
增加和修改的代碼如下:
- var path = window.location.href.replace(/editor/, "UploadFiles");
- // alert(path);
- $.jQRTE.ajaxFileUpload({ url: path, secureuri: false, fileElementId: "upload" + uid + "_fileToUpload", dataType: "json",
4.準(zhǔn)備編輯器頁(yè)面,原代碼如下:
- < %@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
- < asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
- editor
- < /asp:Content>
- < asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
- < link rel="stylesheet" type="text/css" href="css/jqframework.css" mce_href="css/jqframework.css"/>
- < !--[if IE]>< link rel="stylesheet" type="text/css" href="css/ie-only.css" mce_href="css/ie-only.css" />< ![endif]-->
- < link rel="Stylesheet" type="text/css" href="../../Scripts/jqrte/css/jqrte.css" mce_href="Scripts/jqrte/css/jqrte.css" />
- < link type="text/css" href="../../Scripts/jqrte/css/jqpopup.css" mce_href="Scripts/jqrte/css/jqpopup.css" rel="Stylesheet"/>
- < link rel="stylesheet" href="../../Scripts/jqrte/css/jqcp.css" mce_href="Scripts/jqrte/css/jqcp.css" type="text/css"/>
- < mce:script type="text/javascript" src="../../Scripts/jqrte/js/jqDnR.min.js" mce_src="Scripts/jqrte/js/jqDnR.min.js">< /mce:script>
- < mce:script type="text/javascript" src="../../Scripts/jqrte/js/jquery.bgiframe.min.js" mce_src="Scripts/jqrte/js/jquery.bgiframe.min.js">< /mce:script>
- < mce:script type="text/javascript" src="../../Scripts/jqrte/js/jquery.jqcp.min.js" mce_src="Scripts/jqrte/js/jquery.jqcp.min.js">< /mce:script>
- < mce:script type="text/javascript" src="../../Scripts/jqrte/js/jquery.jqpopup.min.js" mce_src="Scripts/jqrte/js/jquery.jqpopup.min.js">< /mce:script>
- < mce:script type="text/javascript" src="../../Scripts/jqrte/js/jquery.jqrte.min.js" mce_src="Scripts/jqrte/js/jquery.jqrte.min.js">< /mce:script>
- < h2>editor< /h2>
- < div id="demo">
- < textarea id="demo1" name="demo1" class="jqrte_popup" rows="5" cols="5" >rich text editor with < b>Content< /b>
這樣就可以在asp.net mvc框架中使用jqrte編輯器的強(qiáng)大功能了。
【編輯推薦】