处理程序.基于ExtJs的TinyMCE文件和图像管理器插件
本文关键字:图像 管理器 插件 文件 TinyMCE 程序 基于 ExtJs 处理 | 更新日期: 2023-09-27 17:50:25
我使用的是Rahul Singla开发的ExtJsFileManagerhttp://www.rahulsingla.com/blog/2011/05/extjsfilemanager-extjs-based-file-and-image-manager-plugin-for-tinymce
这里有Php和c#的例子,并有文件处理程序。我在我的项目中使用Java SpringSource,我是这些技术的新手。我已经尝试过将c#转换为Java,但我不完全理解c#返回给客户端的内容。
下面是c#代码
<%@ WebHandler Language="C#" Class="BrowserHandler" %>
using System;
using System.Web;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using MyCompany;
public class BrowserHandler : IHttpHandler
{
#region Private Members
private HttpContext context;
#endregion
#region IHttpHandler Methods
public void ProcessRequest (HttpContext context)
{
this.context = context;
string op=context.Request["op"];
string path=this.context.Request["path"];
context.Response.ContentType = "text/javascript";
//These are extra parameters you can pass to the server in each request by specifying extjsfilemanager_extraparams option in init.
string param1=context.Request["param1"];
string param2=context.Request["param2"];
switch (op)
{
case "getFolders":
this.getFolders(path);
break;
case "getFiles":
this.getFiles(path);
break;
case "createFolder":
this.createFolder(path);
break;
case "uploadFiles":
this.uploadFiles(path);
break;
}
}
public bool IsReusable
{
get
{
return false;
}
}
#endregion
#region Private Methods
private void getFolders (string path)
{
path = this.validatePath(path);
List<object> l=new List<object>();
foreach (string dir in Directory.GetDirectories(path))
{
l.Add(new
{
text = Path.GetFileName(dir),
path = dir,
leaf = false,
singleClickExpand = true
});
}
this.context.Response.Write(Globals.serializer.Serialize(l));
}
private void getFiles (string path)
{
path = this.validatePath(path);
List<object> l=new List<object>();
foreach (string file in Directory.GetFiles(path))
{
l.Add(new
{
text = Path.GetFileName(file),
virtualPath = Globals.resolveUrl(Globals.resolveVirtual(file))
});
}
this.context.Response.Write(Globals.serializer.Serialize(l));
}
private void createFolder (string path)
{
path = this.validatePath(path);
string name=this.context.Request["name"];
Directory.CreateDirectory(Path.Combine(path, name));
this.context.Response.Write(Globals.serializer.Serialize(new object()));
}
private void uploadFiles (string path)
{
context.Response.ContentType = "text/html";
path = this.validatePath(path);
string successFiles="";
string errorFiles="";
for (int i=0; i < this.context.Request.Files.Count; i++)
{
HttpPostedFile file=this.context.Request.Files[i];
if (file.ContentLength > 0)
{
string fileName=file.FileName;
string extension=Path.GetExtension(fileName);
//Remove .
if (extension.Length > 0) extension = extension.Substring(1);
if (Config.allowedUploadExtensions.Contains(extension, StringComparer.InvariantCultureIgnoreCase))
{
file.SaveAs(Path.Combine(path, fileName));
}
else
{
errorFiles += string.Format("Extension for {0} is not allowed.<br />", fileName);
}
}
}
string s=Globals.serializer.Serialize(new
{
success = true,
successFiles,
errorFiles
});
byte[] b=this.context.Response.ContentEncoding.GetBytes(s);
this.context.Response.AddHeader("Content-Length", b.Length.ToString());
this.context.Response.BinaryWrite(b);
try
{
this.context.Response.Flush();
this.context.Response.End();
this.context.Response.Close();
}
catch (Exception) { }
}
private string validatePath (string path)
{
if (string.IsNullOrEmpty(path))
{
path = Config.baseUploadDirPhysical;
}
if (!path.StartsWith(Config.baseUploadDirPhysical, StringComparison.InvariantCultureIgnoreCase) || (path.IndexOf("..") != -1))
{
throw new SecurityException("Invalid path.");
}
return (path);
}
#endregion}
这是我的代码
@RequestMapping(value = "/filehandler", method = RequestMethod.POST)
public @ResponseBody byte[] filehandler(HttpServletRequest request, HttpServletResponse response) {
String op = request.getParameter("op");
String path = uploadDataFolder; // || request.getParameter("path");
String jsondata = "{root:";
String datastr = "";
File folder = new File(path);
if (op.equals( "getFolders")){
// FOLDERS
for (File fileEntry : folder.listFiles() ) {
//File fileEntry;
if (fileEntry.isDirectory()) {
datastr += "{'"text'":'"" + fileEntry.getName() +
"'",'"path'":'"" + fileEntry.getPath() +
"'",'"leaf'":'"false'"" +
",'"singleClickExpand'":'"true'"}";
}
}
}
else if (op == "getFiles"){
// FILES
for (File fileEntry : folder.listFiles()) {
//File fileEntry;
if (fileEntry.isFile()) {
datastr += "{'"text'":'"" + fileEntry.getName() +
"'",'"virtualPath'":'"" + fileEntry.getPath() + "'"";
} else {
System.out.println(fileEntry.getName());
}
}
}
else if (op == "createFolder"){
// create folder function is not supported in our project
}
else if (op == "uploadFiles"){
// upload images
request.getAttribute("Files");
}
datastr = datastr.equals("") ? "null" : "[" + datastr + "]";
jsondata += datastr + "}";
return getBytes(jsondata);
}
对于getFolders操作,它返回JSON数据,如
{root:[{"text":"blog","path":"C:'workspace'.metadata'.plugins'org.eclipse.wst.server.core'tmp0'wtpwebapps'NetaCommerceFrameworkAdmin'META-INF'static'images'blog","leaf":"false","singleClickExpand":"true"}{"text":"photos","path":"C:'workspace'.metadata'.plugins'org.eclipse.wst.server.core'tmp0'wtpwebapps'NetaCommerceFrameworkAdmin'META-INF'static'images'photos","leaf":"false","singleClickExpand":"true"}]}
我不知道c#代码返回什么,所以我不能用Java写它。它不允许我的JSON返回操作"getFolders"
c# 'HTTPHandler'可以返回它想要的东西,例如,它可以返回图像(一种常用的用法),也可以返回二进制文件数据,以便客户端下载文件。
它们是灵活的,可以配置为"返回"任何东西,因为它们经常直接写入HTTPResponse对象。
为了弄清楚你的处理程序返回什么,你想要寻找任何触及HTTPResponse的东西。
。context.Response.ContentType = "text/javascript";
设置Http响应的内容类型报头。
在那里的几个私有方法似乎生成列表,并返回那些序列化到客户端,看起来只是JSON数据。
这里的例外是uploadFiles()
方法,它似乎接受post请求(您可以告诉,因为它试图访问请求对象以检索上传的文件):
HttpPostedFile file=this.context.Request.Files[i];
经过一些检查后似乎将文件保存到服务器。
如果我是你,我会在Java API中寻找他们自己的序列化器选项,看看是否可以挂钩到它,而不是编写自己的方法来将字符串粘在一起。