FCKeditor 很强大也很讨人喜欢,唯一的缺点就是不支持文件和文件夹的删除,很是遗憾。这篇文章就介绍怎样为 FCKeditor 增加删除功能(基于 C# 的版本)。
FCKeditor 官方网站:http://www.fckeditor.net
本文所针对版本:FCKeditor: 2.6.4,FCKeditor.Net: 2.6.3。
1. 用 Visual Studio 2005/2008 打开项目 FCKeditor.Net 2.6.3,打开文件“FileBrowser/Connector.cs”,为 class Connector 增加如下两个成员函数:
1 |
private void DelFile( XmlNode connectorNode, string resourceType, string currentFolder ) { HttpContext hc = HttpContext.Current; string file = hc.Server.MapPath(hc.Request["FileUrl"]); if (System.IO.File.Exists(file)) System.IO.File.Delete(file); else hc.Response.Write(@"<error number=""1"" originaldescription=""unable to locate file"">"); } private void DelFolder( XmlNode connectorNode, string resourceType, string currentFolder ) { HttpContext hc = HttpContext.Current; string folder = hc.Server.MapPath(hc.Request["FolderName"]); if (System.IO.Directory.Exists(folder)) System.IO.Directory.Delete(folder, true); else hc.Response.Write(@"<error number=""2"" originaldescription=""unable to locate folder"">"); } |
2. 在文件“Connector.cs”中找到 OnLoad 函数,在 switch 部分增加以下红色代码:
1 |
// Execute the required command. switch( sCommand ) { case "GetFolders" : this.GetFolders( oConnectorNode, sResourceType, sCurrentFolder ); break; case "GetFoldersAndFiles" : this.GetFolders( oConnectorNode, sResourceType, sCurrentFolder ); this.GetFiles( oConnectorNode, sResourceType, sCurrentFolder ); break; case "CreateFolder": this.CreateFolder(oConnectorNode, sResourceType, sCurrentFolder); break; <span><font color="#ff0000" face="Courier New">case "DelFile": this.DelFile(oConnectorNode, sResourceType, sCurrentFolder); break; case "DelFolder": this.DelFolder(oConnectorNode, sResourceType, sCurrentFolder); break;</font></span> } |
3. 编译 FCKeditor.net 并关闭该项目。将生成的 FredCK.FCKeditorV2.dll 拷贝出来以备后用。
4. 建立 C# 测试项目,并在其中部署 FCKeditor 2.6.4(使用第 3 步生成的 FredCK.FCKeditorV2.dll)。精简及部署的详细步骤非本文重点,不再重复。
5. 打开“fckeditor/editor/filemanager/browser/default/frmresourceslist.htm”,修改以下两个函数
1 |
oListManager.GetFolderRowHtml = function( folderName, folderPath<font face="Courier New"><span><font color="#ff0000">, folderUrl</font></span> ) { // Build the link to view the folder. var sLink = '<a href="#" onclick="OpenFolder(\'' + ProtectPath(folderPath) + '\');return false;">'; return '<tr>' + '<td width="16">' + sLink + '<img alt="" src="images/Folder.gif" width="16" height="16" border="0"><\/a>' + '<\/td><td nowrap colspan="2"> ' + sLink + folderName + '<\/a>' + '<\/td><span><font color="#ff0000"><td align="right"><a href="#" onclick="DelFolder(\''+folderName+'\',\''+ ProtectPath(folderUrl) + '\');return false;">删除</a></td<span><font color="#ff0000">></font></span><</font></span>\/tr>'; } oListManager.GetFileRowHtml = function( fileName, fileUrl, fileSize ) { // Build the link to view the folder. var sLink = '<a href="#" onclick="OpenFile(\'' + ProtectPath(fileUrl) + '\');return false;">' ; // Get the file icon. va sIcon = oIcons.GetIcon( fileName ) <span><font color="#ff0000"><td align="right"><a href="#" onclick="DelFile(\''+fileName+'\',\'' + ProtectPath(fileUrl) + '\');return false;">删除</a></td></font></span></font><\/tr>'; } |
6. 继续修改文件“frmresourceslist.htm”,在 OpenFile 函数后面增加以下两个函数:
1 |
function DelFile( fileName, fileUrl ) { if (confirm('您确定要删除文件“' + fileName + '”吗?')) oConnector.SendCommand("DelFile", "FileUrl=" + escape(fileUrl), Refresh); } function DelFolder( folderName, folderPath ) { if (confirm('您确定要删除文件夹“' + folderName + '”和里面的所有文件吗?')) oConnector.SendCommand("DelFolder", "FolderName=" + escape(folderPath + folderName), Refresh); } |
7. 继续修改文件“frmresourceslist.htm”,找到 GetFoldersAndFilesCallBa
1 |
oHtml.Append( oListManager.GetFolderRowHtml( sFolderName, sCurrentFolderPath + sFolderName + "/"<span><font color="#ff0000" face="Courier New">, <span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New">sCurrentFolderUrl</font></span> </font></span></font></span></font></span></font></span></font></span></font></span></font></span></font></span>) ); |
至此,删除功能增加完毕,见下图: