asp.net mvc introduces the jquery Jqrte control _ Practical Tips

Source: Internet
Author: User
The main steps are as follows:
1, introduce the Jqrte class library in the ASP.net MVC project to declare that the helper class is used to store information on server-side uploaded files
Copy Code code as follows:

public class Viewdatauploadfilesresult
{
public string Message {get; set;}
public int Length {get; set;}
public string ImagePath {get; set;}
public string Error {get; set;}
}

2, write the processing File Upload Server section code, and the uploaded file information returned to the client, the code is as follows:
Copy Code code as follows:

[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;
}

The reason for such a long time, the problem is mainly here, start with return JSON (R), found that jquery's callback function is always unable to get server-side json, but will jump out of a download file dialog box, read the jquery source code repeatedly, after more than a week of torture, In the asp.net MVC official forum registered a user, after some discussion, finally found that for the Ajax form with the file control, in the action method should be developed JSON contenttype will correctly handle the JSON object, the source code as above, Thanks to the help of enthusiastic friends, or do not know if this problem will torment me when (has been a few days sleep well: (). The original posted links are as follows: http://forums.asp.net/t/1439867.aspx

3. Modify the Jqrte fileupload source code, just change the action path on the line, in jquery.jqrte.min.js modify the Uploads function: Add and modify the code as follows:
Copy Code code as follows:

var path = Window.location.href.replace (/editor/, "uploadfiles");
alert (path);
$.jqrte.ajaxfileupload ({url:path, secureuri:false, Fileelementid: "Upload" + uid + "_filetoupload", DataType: "JSON",

4, Prepare the editor page, the original code is as follows:
Copy Code code as follows:

<%@ 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" href= "Css/jqframework.css"/>
<!--[if Ie]><link rel= "stylesheet" type= "Text/css" href= "Css/ie-only.css" href= "Css/ie-only.css"/><! [endif]-->
<link rel= "Stylesheet" type= text/css "href=". /.. /scripts/jqrte/css/jqrte.css "href=" Scripts/jqrte/css/jqrte.css "/>
<link type= "Text/css" href= ". /.. /scripts/jqrte/css/jqpopup.css "href=" Scripts/jqrte/css/jqpopup.css "rel=" Stylesheet "/>"
<link rel= "stylesheet" href= ". /.. /scripts/jqrte/css/jqcp.css "href=" Scripts/jqrte/css/jqcp.css "type=" Text/css "/>"
<script type= "Text/javascript" src= ". /.. /scripts/jqrte/js/jqdnr.min.js "src=" Scripts/jqrte/js/jqdnr.min.js "></script>
<script type= "Text/javascript" src= ". /.. /scripts/jqrte/js/jquery.bgiframe.min.js "src=" Scripts/jqrte/js/jquery.bgiframe.min.js "></script>
<script type= "Text/javascript" src= ". /.. /scripts/jqrte/js/jquery.jqcp.min.js "src=" Scripts/jqrte/js/jquery.jqcp.min.js "></script>
<script type= "Text/javascript" src= ". /.. /scripts/jqrte/js/jquery.jqpopup.min.js "src=" Scripts/jqrte/js/jquery.jqpopup.min.js "></script>
<script type= "Text/javascript" src= ". /.. /scripts/jqrte/js/jquery.jqrte.min.js "src=" Scripts/jqrte/js/jquery.jqrte.min.js "></script>

<div id= "Demo" >
<textarea id= "Demo1" name= "Demo1" class= "Jqrte_popup" rows= "5" cols= "5" >rich text editor with <b>content </b>
<%@ 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" href= "Css/jqframework.css"/>
<!--[if Ie]><link rel= "stylesheet" type= "Text/css" href= "Css/ie-only.css" href= "Css/ie-only.css"/><! [endif]-->
<link rel= "Stylesheet" type= text/css "href=". /.. /scripts/jqrte/css/jqrte.css "href=" Scripts/jqrte/css/jqrte.css "/>
<link type= "Text/css" href= ". /.. /scripts/jqrte/css/jqpopup.css "href=" Scripts/jqrte/css/jqpopup.css "rel=" Stylesheet "/>"
<link rel= "stylesheet" href= ". /.. /scripts/jqrte/css/jqcp.css "href=" Scripts/jqrte/css/jqcp.css "type=" Text/css "/>"
<script type= "Text/javascript" src= ". /.. /scripts/jqrte/js/jqdnr.min.js "src=" Scripts/jqrte/js/jqdnr.min.js "></script>
<script type= "Text/javascript" src= ". /.. /scripts/jqrte/js/jquery.bgiframe.min.js "src=" Scripts/jqrte/js/jquery.bgiframe.min.js "></script>
<script type= "Text/javascript" src= ". /.. /scripts/jqrte/js/jquery.jqcp.min.js "src=" Scripts/jqrte/js/jquery.jqcp.min.js "></script>
<script type= "Text/javascript" src= ". /.. /scripts/jqrte/js/jquery.jqpopup.min.js "src=" Scripts/jqrte/js/jquery.jqpopup.min.js "></script>
<script type= "Text/javascript" src= ". /.. /scripts/jqrte/js/jquery.jqrte.min.js "src=" Scripts/jqrte/js/jquery.jqrte.min.js "></script>

<div id= "Demo" >
<textarea id= "Demo1" name= "Demo1" class= "Jqrte_popup" rows= "5" cols= "5" >rich text editor with <b>content </b>
Select Format Paragraph Pre Heading 6 Heading 5 Heading 4 Heading 3 Heading 2 Heading 1 Select Font Arial comic Sans Couri Er New Georgia Helvetica Impact times trebuchet Verdana Select Font Size 8 10 12 14 18 24
H:s: L:
R:g: B:
Rows
Columns
Width% pixels
Border
CellSpacing
cellpadding
Alignment Default left Right center
Site Name
Url
Target _blank _parent _self _top
Image URL
Image Description
Alignment left Right
Border
Upload Image
Image Description
Alignment left Right
Border
Upload File
File Name

This makes it possible to use the powerful features of the Jqrte editor in asp.net mvc.

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.