Introduce jquery jqrte control in ASP. net mvc Framework

Source: Internet
Author: User

After nearly two weeks of efforts, I finally introduced JQUERY's JQRTE text editor control to the asp.net mvc framework. The main steps are as follows:

1. Introduce the jqrte class library in the asp.net mvc project and declare that the auxiliary class is used to store the information of files uploaded on the server.

 
 
  1. public class ViewDataUploadFilesResult  
  2. {  
  3.     public string message { get; set; }  
  4.     //public int Length { get; set; }  
  5.     public string imagepath { get; set; }  
  6.     public string error { get; set; }  
  7. }  

2. Write the server segment code for processing file uploads and return the uploaded file information to the client. The Code is as follows:

 
 
  1. [AcceptVerbs(HttpVerbs.Post)]  
  2.         public JsonResult UploadFiles(FormCollection collection)  
  3.         {  
  4.             var r = new ViewDataUploadFilesResult();  
  5.             foreach (string file in Request.Files)  
  6.             {  
  7.                 string url = Request.Url.Authority;  
  8.                 url = "http://" + url;  
  9.                 HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;  
  10.                 string date = DateTime.Now.Date.ToShortDateString();  
  11.                 string path = Path.Combine(  
  12.                     AppDomain.CurrentDomain.BaseDirectory,  
  13.                     "Content");  
  14.                 string datePath = Path.Combine(path,date);  
  15.                 Directory.CreateDirectory(datePath);  
  16.                 url += "/Content/";  
  17.                 url += date;  
  18.                 url += "/";  
  19.                 url += Path.GetFileName(hpf.FileName);  
  20.                 if (hpf.ContentLength == 0)  
  21.                     continue;  
  22.                 string savedFileName = Path.Combine(  
  23.                     datePath,  
  24.                     Path.GetFileName(hpf.FileName));  
  25.                 try 
  26.                 {  
  27.                     hpf.SaveAs(savedFileName);  
  28.                 }  
  29.                 catch (Exception e)  
  30.                 {  
  31.                     r.error = e.ToString();  
  32.                 }  
  33.                 //r.Name = savedFileName;  
  34.                 //r.Length = hpf.ContentLength;  
  35.                 r.imagepath = url;  
  36.                 r.message = "ok";  
  37.                 r.error = "ok";  
  38.                 //r.Add(new ViewDataUploadFilesResult()  
  39.                 //{  
  40.                 //    Name = savedFileName,  
  41.                 //    Length = hpf.ContentLength  
  42.                 //});  
  43.             }  
  44.             JsonResult jsonResult = Json(r);  
  45.             jsonResult.ContentType = "text/html";  
  46.             return jsonResult;  
  47.         }    

After such a long time, the problem mainly exists here. At first, return json (r) is used. It is found that jquery's callback function is always unable to obtain the server-side json, instead, a File Download Dialog Box will pop up and read jquery's source code repeatedly. After a week or so, I registered a user on the official asp.net mvc Forum. After some discussion, finally, we found that for ajax forms with file controls, the json contentType should be set in the action Method to correctly process the json object. The source code is as above. Thanks for your help, otherwise, I don't know when the problem will affect me. Original post link is as follows: http://forums.asp.net/t/1439867.aspx

3. Modify the fileupload source code of jqrte. Just change the action path and modify the uploads function in jquery. jqrte. min. js:

The code added and modified is as follows:

 
 
  1. var path = window.location.href.replace(/editor/, "UploadFiles");     
  2.     //    alert(path);     
  3.     $.jQRTE.ajaxFileUpload({ url: path, secureuri: false, fileElementId: "upload" + uid + "_fileToUpload", dataType: "json",    

4. Prepare the editor page. The original code is as follows:

 
 
  1. < %@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>     
  2.     
  3. < asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">     
  4.     editor     
  5. < /asp:Content>     
  6.     
  7. < asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">     
  8.     < link rel="stylesheet" type="text/css" href="css/jqframework.css" mce_href="css/jqframework.css"/>     
  9.     < !--[if IE]>< link rel="stylesheet" type="text/css" href="css/ie-only.css" mce_href="css/ie-only.css" />< ![endif]-->     
  10.       
  11.     < link rel="Stylesheet" type="text/css" href="../../Scripts/jqrte/css/jqrte.css" mce_href="Scripts/jqrte/css/jqrte.css" />     
  12.     < link type="text/css" href="../../Scripts/jqrte/css/jqpopup.css" mce_href="Scripts/jqrte/css/jqpopup.css" rel="Stylesheet"/>     
  13.     < link rel="stylesheet" href="../../Scripts/jqrte/css/jqcp.css" mce_href="Scripts/jqrte/css/jqcp.css" type="text/css"/>     
  14.     
  15.     < mce:script type="text/javascript" src="../../Scripts/jqrte/js/jqDnR.min.js" mce_src="Scripts/jqrte/js/jqDnR.min.js">< /mce:script>     
  16.     < mce:script type="text/javascript" src="../../Scripts/jqrte/js/jquery.bgiframe.min.js" mce_src="Scripts/jqrte/js/jquery.bgiframe.min.js">< /mce:script>     
  17.     < mce:script type="text/javascript" src="../../Scripts/jqrte/js/jquery.jqcp.min.js" mce_src="Scripts/jqrte/js/jquery.jqcp.min.js">< /mce:script>     
  18.     < mce:script type="text/javascript" src="../../Scripts/jqrte/js/jquery.jqpopup.min.js" mce_src="Scripts/jqrte/js/jquery.jqpopup.min.js">< /mce:script>     
  19.     < mce:script type="text/javascript" src="../../Scripts/jqrte/js/jquery.jqrte.min.js" mce_src="Scripts/jqrte/js/jquery.jqrte.min.js">< /mce:script>     
  20.     < h2>editor< /h2>     
  21.     < div id="demo">     
  22.       < textarea id="demo1" name="demo1" class="jqrte_popup" rows="5" cols="5" >rich text editor with < b>Content< /b>    
  23.   

In this way, the powerful functions of the jqrte editor can be used in the asp.net mvc framework.

  1. ASP. NET shutdown code (Windows is the local machine)
  2. ASP. NET QueryString garbled Solution
  3. ASP. NET screen jump implementation and value passing Solution
  4. ASP. NET Web application user operation Information Description class
  5. The father of ASP. NET is strongly recommended: ASP. NET AJAX

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.