FCKeditor2.66ForASP.NET版上传文件大小如何限制?

lanxing106 2010-04-08 10:09:35
问题同上标题,找了很长时间也没个能行的,高手帮帮搞下!
...全文
171 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
a112814926 2011-08-19
  • 打赏
  • 举报
回复
表示还在纠结中。。。啊……………………………………………………………………
zzxap 2010-04-08
  • 打赏
  • 举报
回复
fckeditor限制上传文件大小的方法
2009-11-11 09:50
以前用fckeditor的时候花了好多时间把FCKeditor.Net_2.6.3的代码都搞明白了,自己也改了些,今天重新要用fckeditor时却有好多又生疏了,还是做点记录吧,文字总是比人的记性好,今天把限制上传文件大小的方法拿出来跟大家分享,下次再发布其他的.(不要问我fckeditor是什么哈!)


由于FCKeditor本身是开源的,所以我们可以对里面的某些代码进行修改.

先到fck官方网站下载FCKeditor.Net_2.6.3.

我用的vs2005版的fck.net , 打开FileWorkerBase.cs

很明显protected void FileUpload( string resourceType, string currentFolder, bool isQuickUpload )是用来控制上传文件的方法, 里面有个if ( oFile == null )

{

this.SendFileUploadResponse( 202, isQuickUpload );

return;

},跟踪调试后发现SendFileUploadResponse是向页面发送各种报告的,最后跟踪到了OnUploadCompleted方法

然后在FCKeditor的页面里搜索OnUploadCompleted,结果找到了fckeditor\editor\dialog\fck_image\fck_image.js,

fckeditor\editor\dialog\fck_link\fck_link.js与fckeditor\editor\dialog\fck_flash\fck_flash.js,他们里面各自都有OnUploadCompleted方法,很明显里面定义了各种编码的报告信息,下面是我改的编码:

switch ( errorNumber )

{

case 0 : // No errors

alert( '您的flash已经成功上传!' ) ;

break ;

case 1 : // Custom error

alert( customMsg ) ;

return ;

case 101 : // Custom warning

alert( customMsg ) ;

break ;

case 201 :

alert( '已经存在相同名字的图片. 您上传的flash将被命名为 "' + fileName + '"' ) ;

break ;

case 202 :

alert( '不支持的文件类型!' ) ;

return ;

case 203 :

alert( "安全警告. 您可能没有权限上传flash. 请联系管理员." ) ;

return ;

case 204 :

alert( "上传flash不能超过150k , 请重新制作后上传!" ) ;

return ;

case 500 :

alert( 'The connector is disabled' ) ;

break ;

default :

alert( 'Error on file upload. Error number: ' + errorNumber ) ;

return ;

}

你可以根据自己的需要增加编码,对应的在FCKeditor.Net_2.6.3.里的FileWorkerBase.cs的FileUpload方法里增加判断就可以了.

比如我现在要限制文件上传大小,在FileUpload里增加判断

if (oFile.ContentLength > 1024 * 150)

{

this.SendFileUploadResponse(204, isQuickUpload);

return;

}

204是我自己定义的错误编码,FCKeditor.Net_2.6.3改好了要生成dll然后在放在项目的bin里

最后在项目里相应的FCKeditor文件里找到前面所述的OnUploadCompleted,加上

case 204 :

alert( "上传flash不能超过150k , 请重新制作后上传!" ) ;

return ;

好了,测试通
qq2013 2010-04-08
  • 打赏
  • 举报
回复
ding
lanxing106 2010-04-08
  • 打赏
  • 举报
回复
我解决了 原来在2.66版本并不是由FCKeditor自己控制文件的上传大小了
而是交给你语言平台本身 我在ASP.NET 的配置一下就好了
lanxing106 2010-04-08
  • 打赏
  • 举报
回复
不行 2.63 和2.66这个类的结构都不一样

/*
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
* Copyright (C) 2003-2007 Frederico Caldeira Knabben
*
* == BEGIN LICENSE ==
*
* Licensed under the terms of any of the following licenses at your
* choice:
*
* - GNU General Public License Version 2 or later (the "GPL")
* http://www.gnu.org/licenses/gpl.html
*
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
* http://www.gnu.org/licenses/lgpl.html
*
* - Mozilla Public License Version 1.1 or later (the "MPL")
* http://www.mozilla.org/MPL/MPL-1.1.html
*
* == END LICENSE ==
*
* Base class used by the FileBrowserConnector and Uploader.
*/

using System;
using System.Web;
using System.Text.RegularExpressions;

namespace FredCK.FCKeditorV2.FileBrowser
{
public abstract class FileWorkerBase : System.Web.UI.Page
{
public Config Config;

protected void FileUpload( string resourceType, string currentFolder, bool isQuickUpload )
{
HttpPostedFile oFile = Request.Files[ "NewFile" ];

string sFileName = "";

if ( oFile == null )
{
this.SendFileUploadResponse( 202, isQuickUpload );
return;
}

// Map the virtual path to the local server path.
string sServerDir = this.ServerMapFolder( resourceType, currentFolder, isQuickUpload );

// Get the uploaded file name.
sFileName = System.IO.Path.GetFileName( oFile.FileName );
sFileName = this.SanitizeFileName( sFileName );

string sExtension = System.IO.Path.GetExtension( oFile.FileName );
sExtension = sExtension.TrimStart( '.' );

if ( !this.Config.TypeConfig[ resourceType ].CheckIsAllowedExtension( sExtension ) )
{
this.SendFileUploadResponse( 202, isQuickUpload );
return;
}

if ( this.Config.CheckIsNonHtmlExtension( sExtension ) && !this.CheckNonHtmlFile( oFile ) )
{
this.SendFileUploadResponse( 202, isQuickUpload );
return;
}

int iErrorNumber = 0;
int iCounter = 0;

while ( true )
{
string sFilePath = System.IO.Path.Combine( sServerDir, sFileName );

if ( System.IO.File.Exists( sFilePath ) )
{
iCounter++;
sFileName =
System.IO.Path.GetFileNameWithoutExtension( oFile.FileName ) +
"(" + iCounter + ")." +
sExtension;

iErrorNumber = 201;
}
else
{
oFile.SaveAs( sFilePath );
break;
}
}

TypeConfig typeConfig = this.Config.TypeConfig[resourceType] ;

string sFileUrl = isQuickUpload ? typeConfig.GetQuickUploadPath() : typeConfig.GetFilesPath() ;
sFileUrl += sFileName;

this.SendFileUploadResponse( iErrorNumber, isQuickUpload, sFileUrl, sFileName );
}

private void SendFileUploadResponse( int errorNumber, bool isQuickUpload )
{
this.SendFileUploadResponse( errorNumber, isQuickUpload, "", "", "" );
}

private void SendFileUploadResponse( int errorNumber, bool isQuickUpload, string fileUrl, string fileName )
{
this.SendFileUploadResponse( errorNumber, isQuickUpload, fileUrl, fileName, "" );
}

protected void SendFileUploadResponse( int errorNumber, bool isQuickUpload, string fileUrl, string fileName, string customMsg )
{
Response.Clear();

Response.Write( "<script type=\"text/javascript\">" );

// Minified version of the document.domain automatic fix script.
// The original script can be found at _dev/domain_fix_template.js
Response.Write( @"(function(){var d=document.domain;while (true){try{var A=window.top.opener.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();" );

if ( isQuickUpload )
Response.Write( "window.parent.OnUploadCompleted(" + errorNumber + ",'" + fileUrl.Replace( "'", "\\'" ) + "','" + fileName.Replace( "'", "\\'" ) + "','" + customMsg.Replace( "'", "\\'" ) + "') ;" );
else
Response.Write( "window.parent.frames['frmUpload'].OnUploadCompleted(" + errorNumber + ",'" + fileName.Replace( "'", "\\'" ) + "') ;" );

Response.Write( "</script>" );

Response.End();
}

protected string ServerMapFolder( string resourceType, string folderPath, bool isQuickUpload )
{
TypeConfig typeConfig = this.Config.TypeConfig[ resourceType ];

// Get the resource type directory.
string sResourceTypePath = isQuickUpload ? typeConfig.GetQuickUploadDirectory() : typeConfig.GetFilesDirectory();

// Ensure that the directory exists.
Util.CreateDirectory( sResourceTypePath );

// Return the resource type directory combined with the required path.
return System.IO.Path.Combine( sResourceTypePath, folderPath.TrimStart( '/' ) );
}


// Do a cleanup of the folder name to avoid possible problems
protected string SanitizeFolderName( string folderName )
{
// Remove . \ / | : ? * " < >
return Regex.Replace( folderName, @"[.\\/|:?*""<>\p{C}]", "_", RegexOptions.None );
}

// Do a cleanup of the file name to avoid possible problems
private string SanitizeFileName( string fileName )
{
// Replace dots in the name with underscores (only one dot can be there... security issue).
if ( Config.ForceSingleExtension )
fileName = Regex.Replace( fileName, @"\.(?![^.]*$)", "_", RegexOptions.None );

// Remove \ / | : ? * " < >
return Regex.Replace( fileName, @"[\\/|:?*""<>\p{C}]", "_", RegexOptions.None );
}

private bool CheckNonHtmlFile( HttpPostedFile file )
{
byte[] buffer = new byte[ 1024 ];
file.InputStream.Read(buffer, 0, 1024);

string firstKB = System.Text.ASCIIEncoding.ASCII.GetString( buffer );

if ( Regex.IsMatch( firstKB, @"<!DOCTYPE\W*X?HTML", RegexOptions.IgnoreCase | RegexOptions.Singleline ) )
return false;

if ( Regex.IsMatch( firstKB, @"<(?:body|head|html|img|pre|script|table|title)", RegexOptions.IgnoreCase | RegexOptions.Singleline ) )
return false;

//type = javascript
if ( Regex.IsMatch( firstKB, @"type\s*=\s*[\'""]?\s*(?:\w*/)?(?:ecma|java)", RegexOptions.IgnoreCase | RegexOptions.Singleline ) )
return false;

//href = javascript
//src = javascript
//data = javascript
if ( Regex.IsMatch( firstKB, @"(?:href|src|data)\s*=\s*[\'""]?\s*(?:ecma|java)script:", RegexOptions.IgnoreCase | RegexOptions.Singleline ) )
return false;

//url(javascript
if ( Regex.IsMatch( firstKB, @"url\s*\(\s*[\'""]?\s*(?:ecma|java)script:", RegexOptions.IgnoreCase | RegexOptions.Singleline ) )
return false;

return true;
}
}
}

丰云 2010-04-08
  • 打赏
  • 举报
回复
lanxing106 2010-04-08
  • 打赏
  • 举报
回复
狂顶 zzxap

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧