For ASP.NET 的修改相对其他语言较麻烦一些,上传部分都编译到 DLL 文件里面了,只能通过修改源代码,再重新编译后方能使用。上传其实分2部分,一是“快速上传”,即在网页对话框中点“上传”标签后出来的界面,通过这里上传的文件都会存放在 UserFiles 根目录,如下图:
还有一个上传在点击“浏览服务器”后出来的界面中,我称之为“选择性上传”,这部分上传的文件则会根据用户选择的不同资源存放在相应的目录,或是 Image 或是 Flash、File等等。
用 VS.NET 打开 FredCK.FCKeditorV2.csproj,打开 Uploader.cs 文件,通过文件开头的注释:This is the code behind of the uploader.aspx page used for Quick Uploads.可以得知该文件是控制“快速上传”的,找到以下代码:
while ( true )
{
string sFilePath = System.IO.Path.Combine( this.UserFilesDirectory, sFileName ) ;
if ( System.IO.File.Exists( sFilePath ) )
{
iCounter++ ;
sFileName =
System.IO.Path.GetFileNameWithoutExtension( oFile.FileName ) +
"(" + iCounter + ")" +
System.IO.Path.GetExtension( oFile.FileName ) ;
iErrorNumber = 201 ;
}
else
{
oFile.SaveAs( sFilePath ) ;
sFileUrl = this.UserFilesPath + sFileName ;
break ;
}
}
大意是如果存在同名文件则在文件名后加入 (n) 来标识,否则以原文件名保存。把它改成不论有没有同名文件,都重新生成一个新的文件名。修改后的代码变成:
{
// Rebuild file name.
sFileName = DateTime.Now.ToString().Replace("-","").Replace(" ","").Replace(":","")+System.IO.Path.GetExtension(oFile.FileName);
string sFilePath = System.IO.Path.Combine( this.UserFilesDirectory, sFileName ) ;
oFile.SaveAs( sFilePath ) ;
sFileUrl = this.UserFilesPath + sFileName ;
break ;
}
DataTime.Now.ToString()是获取当前时间并转换为字符串,得到的结果以 YYYY-MM-DD HH:MM:SS 的形式出现,用 Replace 把其中的分隔符及空格替换掉;再加上原文件的扩展名,“快速上传”修改完成。上传后的文件以 2005126152012.jpg 的命名方式存放到服务器上。
再打开 FileBrowserConnector.cs 文件,找到 Command Handlers 区中的 FileUpload 函数段,同样是修改 while 语句中的内容:
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 + ")" +
System.IO.Path.GetExtension( oFile.FileName ) ;
sErrorNumber = "201" ;
}
else
{
oFile.SaveAs( sFilePath ) ;
break ;
}
}
把它改成:
{
// Rebuild file name.
sFileName = DateTime.Now.ToString().Replace("-","").Replace(" ","").Replace(":","")+System.IO.Path.GetExtension(oFile.FileName);
string sFilePath = System.IO.Path.Combine( sServerDir, sFileName ) ;
oFile.SaveAs( sFilePath ) ;
break ;
}
因为不再使用 iCounter 变量,所以一并去除。保存文件,重新生成解决方案;打开网站项目,在引用中删除原来的 FredCK.FCKeditorV2,再把刚才生成的 FredCK.FCKeditorV2.dll 文件复制到网站 bin 目录下,重新添加引用、生成,就大功告成了。