时间:2021-07-01 10:21:17 帮助过:3人阅读
同时设置utf-8编码,确保中文不乱码.
$.base64.utf8encode = true;
var editor = "<input type=‘hidden‘ name=‘" + $this.attr("name") + "‘ value=‘"
+ $.base64.btoa($this.html()) + "‘ />";
关键代码:将富文本对象的html值转换为base64,然后封装到表单form中。
详细见如下(一整个form提交的表单封装,参照与dwz框架):
/**
* 带文件上传的ajax表单提交
*
* @param {Object}
* form
* @param {Object}
* callback
*/
function iframeCallback(form, callback) {
YUNM.debug("带文件上传处理");
var $form = $(form), $iframe = $("#callbackframe");
// 富文本编辑器
$("div.editor", $form).each(
function() {
var $this = $(this);
var editor = "<input type=‘hidden‘ name=‘" + $this.attr("name") + "‘ value=‘"
+ $.base64.btoa($this.html()) + "‘ />";
$form.append(editor);
});
var data = $form.data(‘bootstrapValidator‘);
if (data) {
if (!data.isValid()) {
return false;
}
}
if ($iframe.size() == 0) {
$iframe = $("<iframe id=‘callbackframe‘ name=‘callbackframe‘ src=‘about:blank‘ style=‘display:none‘></iframe>")
.appendTo("body");
}
if (!form.ajax) {
$form.append(‘<input type="hidden" name="ajax" value="1" />‘);
}
form.target = "callbackframe";
_iframeResponse($iframe[0], callback || YUNM.ajaxDone);
}
function _iframeResponse(iframe, callback) {
var $iframe = $(iframe), $document = $(document);
$document.trigger("ajaxStart");
$iframe.bind("load", function(event) {
$iframe.unbind("load");
$document.trigger("ajaxStop");
if (iframe.src == "javascript:‘%3Chtml%3E%3C/html%3E‘;" || // For
// Safari
iframe.src == "javascript:‘<html></html>‘;") { // For FF, IE
return;
}
var doc = iframe.contentDocument || iframe.document;
// fixing Opera 9.26,10.00
if (doc.readyState && doc.readyState != ‘complete‘)
return;
// fixing Opera 9.64
if (doc.body && doc.body.innerHTML == "false")
return;
var response;
if (doc.XMLDocument) {
// response is a xml document Internet Explorer property
response = doc.XMLDocument;
} else if (doc.body) {
try {
response = $iframe.contents().find("body").text();
response = jQuery.parseJSON(response);
} catch (e) { // response is html document or plain text
response = doc.body.innerHTML;
}
} else {
// response is a xml document
response = doc;
}
callback(response);
});
}
$(‘#editor‘).html($.base64.atob(description, true));
通过base64对数据库中保存的html代码进行解码。
关于wysiwyg组件封装代码,我已上传到CSDN的代码库,可详细参照。
function html_encode(str) {
var s = "";
if (str.length == 0)
return "";
s = str.replace(/&/g, ">");
s = s.replace(/</g, "<");
s = s.replace(/>/g, ">");
s = s.replace(/ /g, " ");
s = s.replace(/\‘/g, "'");
s = s.replace(/\"/g, """);
s = s.replace(/\n/g, "<br>");
return s;
}
function html_decode(str) {
var s = "";
if (str.length == 0)
return "";
s = str.replace(/>/g, "&");
s = s.replace(/</g, "<");
s = s.replace(/>/g, ">");
s = s.replace(/ /g, " ");
s = s.replace(/'/g, "\‘");
s = s.replace(/"/g, "\"");
s = s.replace(/<br>/g, "\n");
return s;
}
一般情况下,使用上面两个方法对html数据进行编码和解码,但是对于图片的保存却无能为力。
笑对现实的无奈,不能后退的时候,不再傍徨的时候,永远向前 路一直都在──陈奕迅《路一直都在》
本文出自:【沉默王二的博客】
wysiwyg富文本数据如何保存到mysql
标签: