从目录中 json.net 对象
json.net object from directory
Tinymce是一个相当常见的所见即所得编辑器,允许用户购买图像/文件管理系统或构建自己的系统。 我想将我的内容管理系统从 2 倍升级到 4 倍。 在 4x 中,图像需要作为 image_list 声明中的 json 对象呈现给 tinymce 调用:http://www.tinymce.com/wiki.php/Configuration:image_list
基于这种格式,目标是让 .Net 读取图像文件夹,并将标题和值显示为每个文件的文件名和"图像/文件名"。 在过去的几天里,我一直在努力,取得了一些进展,但还没有达到功能样本的地步。 这似乎是使用 .Net 和 Tinymce 的其他人想要使用的东西,如果他们推出自己的图像管理系统(在这种情况下我必须这样做(。
此示例(请参阅链接(似乎很接近,但我的测试(请参阅下面的代码(返回标题和值的 null 值。 当我尝试在示例中包含其他两个变量时,我收到错误消息,指出变量无法访问或不可用。在 C# 中将文件列表转换为 JSON 数组
以下是我尝试用于创建图像引用的 JSON 变量的 C# 脚本:
<%@ Import Namespace="Newtonsoft.Json" %>
<script runat="server">
public class FileInformation
{
[JsonProperty(PropertyName = "title")]
public string actualFileName {get;set;}
public string value {get;set;}
}
public string image_list()
{
string[] arr = new string[3]; // Initialize
arr[0] = ".jpg"; // Element 1
arr[1] = ".png"; // Element 2
arr[2] = ".gif"; // Element 3
var imgPath = Server.MapPath("~/images/");
var list = new List<FileInformation>();
//string[] fileNames = Directory.GetFiles(imgPath, "*", SearchOption.TopDirectoryOnly);
string[] fileNames = Directory.GetFiles(imgPath);
foreach (string filename in fileNames)
{
FileInfo fileInfo = new FileInfo(filename);
string actualFileName = fileInfo.Name;
string value = fileInfo.Name;
list.Add(new FileInformation(){});
}
var yourJSONString = JsonConvert.SerializeObject(list);
return yourJSONString;
}
...
</script>
以下是页面中的 Javascript 调用:
<script type="text/javascript">
tinymce.init({
image_list: '<%= image_list()%>',
image_class_list: [
{title: 'None', value: ''},
{title: 'Float Right', value: 'img_fright'}
]
});
来自呈现image_list的 pate 的缩写源代码:
image_list: '[{"title":null,"value":null},{"title":null,"value":null},{"title":null,"value":null}]',
有什么想法可能出错吗?
抱歉...不太确定如何编辑答案。 这个更清楚一点,可用于评论。 这个实际上过滤掉了您可能想要执行的特定扩展,也可以用于文档或媒体列表(经过一些修改(。 祝你好运。 我在这里学到了很多东西,在可以的时候回馈是件好事。
public class FileInformation
{
public string title {get;set;}
public string value {get;set;}
}
public string image_list()
{
//Set up the list of acceptible extensions
string[] arr = new string[3]; // Initialize
arr[0] = ".jpg"; // Element 1
arr[1] = ".png"; // Element 2
arr[2] = ".gif"; // Element 3
//Declare the variable
var filePath = "";
//Set the path
filePath = Server.MapPath("~/images/");
//Start the string for the list
var list = new List<FileInformation>();
//Get the filesnames from the path
string[] fileNames = Directory.GetFiles(filePath, "*", SearchOption.TopDirectoryOnly);
//Loop through each of the file names
foreach (string filename in fileNames)
{
//Get the information on the filename
FileInfo fileInfo = new FileInfo(filename);
//Loop through each of extension provided
foreach (var ext in arr) {
//If the extenion on the filename matches one of the list then...
if (fileInfo.Extension == ext) {
//Add the filename and location to the list in the title: filename, value: "images/filename" format
list.Add(new FileInformation(){ title = fileInfo.Name, value = "images/" + fileInfo.Name });
}
}
}
//Convert the list to a JSON string using JSON.net (use the correct framework format)
var yourJSONString = JsonConvert.SerializeObject(list);
//Return the JSON string for use in the javascript call.
return yourJSONString;
}
。以及文件中的 javascript:
<script src="//tinymce.cachefly.net/4.1/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({
selector: "textarea",
theme: "modern",
encoding: 'xml',
convert_urls: false,
removed_menuitems: 'newdocument',
plugins: [
"advlist autolink lists link image charmap print preview hr anchor pagebreak",
"searchreplace wordcount visualblocks visualchars code fullscreen",
"insertdatetime media nonbreaking save table contextmenu directionality",
"template paste textpattern"
],
toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright | bullist numlist outdent indent | link image | print preview media",
content_css: "",
style_formats: [{ title: 'Bold', inline: 'span', classes: 'Bold' },
{ title: 'Italic', inline: 'span', classes: 'Italic' },
{ title: 'Superscript', inline: 'span', classes: 'Superscript' },
{ title: 'Subscript', inline: 'span', classes: 'Subscript' }],
document_base_url: '',
image_list: <%= image_list()%>,
image_class_list: [
{title: 'None', value: ''},
{title: 'Float Right', value: 'img_fright'}
],
cleanup: false,
image_advtab: true,
templates: [
{title: 'Test template 1', content: '<div row><div class="col-md-6">Content</div><div class="col-md-6">Content</div></div>'},
{title: 'Test template 2', content: 'Test 2'}
]
});
如果我将 json 写入文件,我计划发布代码,因为我希望能够动态加载模板并使模板可供我们的用户使用。 如果您有任何示例,请随时在此处发布。 否则祝你好运!
- ASP.net使对象可点击
- 如何从jQuery对象文本正确调用服务器端ASP.NET并将数据返回给jQuery
- 使用查询将对象数组发布到asp.net-mvc控制器操作的正确方法是什么
- 意外结果,在 ASP.Net 中解析 JSON 对象的数组
- 从目录中 json.net 对象
- Breeze.js/NET+EF复杂的对象行为相当奇怪
- 谷歌地图-来自VB.Net的JSON对象标记
- 如何从C#.Net Webbrowser对象访问javascript变量
- 使用javascript将JSON对象绑定到asp.net gridview控件
- jQuery:内容页面中预期的对象 ASP.NET
- asp.net 文字对象传递给 JavaScript
- 我们如何在 lift 中使用 net.liftweb.http.js.JsCmds.Script 对象
- 在 Telerik ASP.Net UI 客户端中,如果我以内联方式附加客户端事件,如何获取鼠标事件对象
- 使用具有有效 JavaScript 格式的 Json.NET 序列化对象
- ASP.NET 控制器中的 JSON 对象进行查看
- 如何在 vb.net 中将对象转换为数组
- 在视图中将.net对象转换为JSON对象
- 工具转换asp.net对象到javascript对象
- 如何处理Json.. NET对象引用
- 在WP7 WebBrowser控件中从Javascript调用.net对象