主要步骤如下:
1,在asp.net mvc项目中引入jqrte类库,声明辅助类用于存储服务器端上载文件的信息
复制代码 代码如下:
public class ViewDataUploadFilesResult
{
public string message { get; set; }
//public int Length { get; set; }
public string imagepath { get; set; }
public string error { get; set; }
}
2,编写处理文件上载服务器段代码,并将上载的文件信息返回给客户端,代码如下:
复制代码 代码如下:
[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;
}
之所以搞了这么长时间,问题也主要出在这儿,开始用的是return json(r),发现jquery的回调函数总是无法获得服务器端的json,反而会跳出个下载文件对话框,反复阅读jquery的源代码,折磨了一周多后,在asp.net mvc官方论坛上注册了个用户,经过一番讨论,最后发现对于有file控件的ajax form,在action方法中应当制定json的contentType才会正确处理json对象,源代码如上,感谢热心朋友的帮助,要不然不知道这个问题会折磨到我什么时候(已经好几天睡不好觉了:()。原贴链接如下:http://forums.asp.net/t/1439867.aspx
3.修改jqrte的fileupload源代码,只要改一下action路径就行,在jquery.jqrte.min.js中修改uploads函数: 增加和修改的代码如下:
复制代码 代码如下:
var path = window.location.href.replace(/editor/, "UploadFiles");
// alert(path);
$.jQRTE.ajaxFileUpload({ url: path, secureuri: false, fileElementId: "upload" + uid + "_fileToUpload", dataType: "json",
4,准备编辑器页面,原代码如下:
复制代码 代码如下:
<%@ 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="/UploadFiles/2021-04-02/jqDnR.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.bgiframe.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.jqcp.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.jqpopup.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.jqrte.min.js"><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>
<%@ 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="/UploadFiles/2021-04-02/jqDnR.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.bgiframe.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.jqcp.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.jqpopup.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.jqrte.min.js"><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>
Select Format Paragraph Pre Heading 6 Heading 5 Heading 4 Heading 3 Heading 2 Heading 1 Select Font Arial Comic Sans Courier 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
这样就可以在asp.net mvc中使用jqrte编辑器的强大功能了
1,在asp.net mvc项目中引入jqrte类库,声明辅助类用于存储服务器端上载文件的信息
复制代码 代码如下:
public class ViewDataUploadFilesResult
{
public string message { get; set; }
//public int Length { get; set; }
public string imagepath { get; set; }
public string error { get; set; }
}
2,编写处理文件上载服务器段代码,并将上载的文件信息返回给客户端,代码如下:
复制代码 代码如下:
[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;
}
之所以搞了这么长时间,问题也主要出在这儿,开始用的是return json(r),发现jquery的回调函数总是无法获得服务器端的json,反而会跳出个下载文件对话框,反复阅读jquery的源代码,折磨了一周多后,在asp.net mvc官方论坛上注册了个用户,经过一番讨论,最后发现对于有file控件的ajax form,在action方法中应当制定json的contentType才会正确处理json对象,源代码如上,感谢热心朋友的帮助,要不然不知道这个问题会折磨到我什么时候(已经好几天睡不好觉了:()。原贴链接如下:http://forums.asp.net/t/1439867.aspx
3.修改jqrte的fileupload源代码,只要改一下action路径就行,在jquery.jqrte.min.js中修改uploads函数: 增加和修改的代码如下:
复制代码 代码如下:
var path = window.location.href.replace(/editor/, "UploadFiles");
// alert(path);
$.jQRTE.ajaxFileUpload({ url: path, secureuri: false, fileElementId: "upload" + uid + "_fileToUpload", dataType: "json",
4,准备编辑器页面,原代码如下:
复制代码 代码如下:
<%@ 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="/UploadFiles/2021-04-02/jqDnR.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.bgiframe.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.jqcp.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.jqpopup.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.jqrte.min.js"><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>
<%@ 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="/UploadFiles/2021-04-02/jqDnR.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.bgiframe.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.jqcp.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.jqpopup.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.jqrte.min.js"><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>
Select Format Paragraph Pre Heading 6 Heading 5 Heading 4 Heading 3 Heading 2 Heading 1 Select Font Arial Comic Sans Courier 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
这样就可以在asp.net mvc中使用jqrte编辑器的强大功能了
标签:
ASP.NET,MVC
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“ASP.NET MVC引入JQUERY JQRTE控件”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
2024年11月09日
2024年11月09日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]