使用CKEditor自定义文件浏览器,并使用ASP.Net MVC进行上传
Using CKEditor custom filebrowser and upload with ASP.Net MVC
我有一个MVC应用程序,我正试图与它一起使用CKEditor。我看到的一个例子是这里,但还有很多其他的例子。到目前为止还不错,但有一部分我仍然很好奇,那就是js,它将所选文件名发送回文件上传对话框文本框。
<script type="text/javascript">
$(document).ready(function () {
$(".returnImage").click("click", function (e) {
var urlImage = $(this).attr("data-url");
window.opener.updateValue("cke_72_textInput", urlImage);
window.close();
});
});
</script>
特别是cke_72_textInput
元素。我的例子一开始并不起作用,直到我打开chrome-dev工具,找到了文本输入的实际id,在我的例子中是cke_76_textinput。我想知道为什么身份证会改变?用这样一个特定的id来指代似乎有点"脆弱"?上面的js代码只是获取所选的图像文件,并将其返回到fileupload对话框的文本框中。
是否有公开的东西间接引用了这个文本框元素,而没有通过id指定它(例如通过config)?
查看:
$(document).ready(function () {
CKEDITOR.replace('Text-area-name', {
filebrowserImageUploadUrl: '/Controller-name/UploadImage'
});
CKEDITOR.editorConfig = function (config) {
// Define changes to default configuration here. For example:
config.language = 'de';
// config.extraPlugins = 'my_own_plugin'; // if you have any plugin
// config.uiColor = '#AADC6E';
// config.image_previewText = CKEDITOR.tools.repeat(' Hier steht dann dein guter Text. ', 8 );
// config.contentsLanguage = 'de';
config.height = 350; // 350px, specify if you want a larger height of the editor
config.linkShowAdvancedTab = false;
config.linkShowTargetTab = false;
};
CKEDITOR.on('dialogDefinition', function (ev) {
var dialogName = ev.data.name;
var dialogDefinition = ev.data.definition;
ev.data.definition.resizable = CKEDITOR.DIALOG_RESIZE_NONE;
if (dialogName == 'link') {
var infoTab = dialogDefinition.getContents('info');
infoTab.remove('protocol');
dialogDefinition.removeContents('target');
dialogDefinition.removeContents('advanced');
}
if (dialogName == 'image') {
dialogDefinition.removeContents('Link');
dialogDefinition.removeContents('advanced');
var infoTab = dialogDefinition.getContents('info');
infoTab.remove('txtBorder');
infoTab.remove('txtHSpace');
infoTab.remove('txtVSpace');
infoTab.remove('cmbAlign');
}
});
}
在控制器上:
[HttpPost]
public ActionResult UploadImage(HttpPostedFileBase file, string CKEditorFuncNum, string CKEditor, string langCode)
{
if (file.ContentLength <= 0)
return null;
// here logic to upload image
// and get file path of the image
const string uploadFolder = "Assets/img/";
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath(string.Format("~/{0}", uploadFolder)), fileName);
file.SaveAs(path);
var url = string.Format("{0}{1}/{2}/{3}", Request.Url.GetLeftPart(UriPartial.Authority),
Request.ApplicationPath == "/" ? string.Empty : Request.ApplicationPath,
uploadFolder, fileName);
// passing message success/failure
const string message = "Image was saved correctly";
// since it is an ajax request it requires this string
var output = string.Format(
"<html><body><script>window.parent.CKEDITOR.tools.callFunction({0}, '"{1}'", '"{2}'");</script></body></html>",
CKEditorFuncNum, url, message);
return Content(output);
}
我遇到了同样的问题。。。考虑到这似乎是一个常见的用例,我找不到任何官方文档,这让我有点沮丧。
无论如何,请看一下这里的快速教程:http://r2d2.cc/2010/11/03/file-and-image-upload-with-asp-net-mvc2-with-ckeditor-wysiwyg-rich-text-editor/.万一链接断了,下面是我所做的。
[HttpPost]
public ActionResult UploadImage(HttpPostedFileBase upload, string ckEditorFuncNum)
{
/*
add logic to upload and save image here
*/
var path = "~/Path/To/image.jpg"; // Logical relative path to uploaded image
var url = string.Format("{0}://{1}{2}",
Request.Url.Scheme,
Request.Url.Authority,
Url.Content(path)); // URL path to uploaded image
var message = "Saved!"; // Optional
var output = string.Format("<script>window.parent.CKEDITOR.tools.callFunction({0}, '{1}', '{2}');</script>",
CKEditorFuncNum,
url,
message);
return Content(output);
}
相关文章:
- 将Javascript数组发送到控制器ASP.NET MVC
- 什么'是在asp.net MVC中将本地化的resources.resx文件转换为javascript文件的有效
- C#asp.net mvc Set CheckBoxFor已检查的具有Model Value的属性
- ASP.NET MVC 3-在ajax调用后,重定向到新页面或生成页面刷新
- 在创建视图mvc.net的过程中使用javascript
- MVC.Net 4 Razor问题使用<文本>标签
- 将文件异步上传到 MVC.NET
- 无论如何 MVC.Net Cdn 调用回退
- 如何加快从Angular/Mvc.net加载图像的速度
- 如何使用角度模块“ui-route”在指令中查找模板URL,而不会在MVC .NET上出现错误
- 将MVC.NET Razor与Javascript结合起来构建一个数组
- MVC.NET提示用户登录
- 从mvc.net获取用于AngularJS http的url的健壮方法是什么?
- MVC.net 3 -可维护的,重构友好的Javascript文件
- 在c#代码MVC . net中检索html中的id元素
- MVC.NET中引导日期拾取器的问题
- MVC . net 4.6询问用户是否想要运行JS文件
- 要求在MVC . net中加载竞速问题
- asp.net mvc - .NET MVC:查找默认的 Bootsrap JavaScript
- 在MVC.NET中使用html helper编辑模式